VBA从Web服务器导入UTF-8 CSV文件

时间:2014-05-13 08:48:49

标签: excel vba csv utf-8

我有一个存储在Web服务器上的UTF-8 CSV文件。当我下载文件时将它放在我的硬盘上,然后我将它导入带有此宏的Excel工作表(来自宏录制器):

Sub Macro2()
Workbooks.OpenText Filename:= _
    "C:/myFile.csv", Origin _
    :=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
    , Comma:=True, Space:=False, Other:=False
End Sub

正确显示所有字符(越南字符)。

当我尝试相同的宏而不是给出文件的本地地址(“C:/myFile.csv”)时,我传递了文件的URL(“http://myserver.com/myFile.csv”),CSV被正确导入进入我的Excel表格,但越南字符不再正确显示。

我也尝试使用“数据”选项卡,但Excel似乎忽略了编码:

With ActiveSheet.QueryTables.Add(Connection:= _
                "TEXT;C:/myFile.csv" _
                , Destination:=Range("$A$1"))
                .Name = "myFile.csv"
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .TextFilePromptOnRefresh = False
                .TextFilePlatform = 65001
                .TextFileStartRow = 1
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = True
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = False
                .TextFileSpaceDelimiter = False
                .TextFileOtherDelimiter = "~"
                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
       End With

示例数据:„; Â; ˜; Â1/4; ‰; ™,™

哪个Excel读错了:„; Â; ˜; Â1/4; ‰; ™,™;

3 个答案:

答案 0 :(得分:7)

如果您自己下载csv文件时字符显示正确,我将该过程分为两个阶段:

下载

Sub DownloadFile(ByVal url As String, ByVal local As String)

Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", url, False, "username", "password"
WinHttpReq.send

myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.responseBody
    oStream.SaveToFile local, 2 
    oStream.Close
End If

End Sub

正在加载CSV

Sub OpenCsv(ByVal csvfile As String)
Workbooks.OpenText Filename:= _ 
csvfile,Local:=True,StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
, Comma:=True, Space:=False, Other:=False
End Sub

注意: Local参数是此处的关键,它使VBA使用您的excel的本地配置(越南语),默认情况下设置到False

全部放在一起

Sub DownloadAndLoad
  DownloadFile "http://myserver.com/myFile.csv","C:\myFile.csv"
  OpenCsv "C:\myFile.csv"
End Sub

答案 1 :(得分:5)

我一直在研究类似的问题,我们将utf-8编码的csv文件导入到工作表中。我不是从Web服务器提取数据,但这可能会有所帮助。

我的解决方案是将utf-8文件读取到局部变量,然后将其插入到工作表中。我尝试将数据保存到带有ansi编码的临时文件中,但这样做会导致所有字符都失去重音。

Function ReadUTF8CSVToSheet(file As String)
    Dim ws As Worksheet
    Dim strText As String

    ' read utf-8 file to strText variable
   With CreateObject("ADODB.Stream")
        .Open
        .Type = 1  ' Private Const adTypeBinary = 1
        .LoadFromFile file
        .Type = 2  ' Private Const adTypeText = 2
        .Charset = "utf-8"
        strText = .ReadText(-1)  ' Private Const adReadAll = -1
    End With

    ' parse strText data to a sheet
    Set ws = Sheets.Add()
    intRow = 1
    For Each strLine In Split(strText, chr(10))
        If strLine <> "" Then
            With ws
                .Cells(intRow, 1) = strLine
                .Cells(intRow, 1).TextToColumns Destination:=Cells(intRow, 1), DataType:=xlDelimited, _
                    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
                    Semicolon:=False, Comma:=True, Space:=False, Other:=False
            End With

            intRow = intRow + 1
        End If
    Next strLine

    ReadUTF8CSVToSheet = ws.Name

End Function

' to run
strSheetName = ReadUTF8CSVToSheet("C:\temp\utf8file.csv")

答案 2 :(得分:3)

IMO,当使用录制的宏代码打开UTF-8 / UTF-8-BOM文件时,Excel中似乎存在错误/冲突,特别是当Origin参数设置为65001假设是UTF-8。

我找到了两个解决此问题的方法:

  1. 从函数调用中删除Origin参数,查看文件是否正确加载Workbooks.OpenText Filename:="C:\file.csv"

    MSDN says

      

    如果省略此参数,则该方法使用当前设置   文本导入向导中的文件原点选项。

    我认为只要您将文件与Excel链接,它就应该尝试读取文件的标题并自动选择正确的Country Code好吧,假设标题没有丢失< / em>的)。

  2. 我尝试了不同的Country Codes,发现在我的特定情况下设置Origin:=1252 1252 - windows-1252 - ANSI Latin 1; Western European (Windows) )在Excel中加载文件就好了。