首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >VBA从web服务器导入UTF-8 CSV文件

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

Stack Overflow用户
提问于 2014-05-13 16:48:49
回答 1查看 15.2K关注 0票数 19

我在web服务器上存储了一个UTF-8 CSV文件。当我下载文件时,把它放到我的硬盘上,然后用这个宏将它导入到Excel工作表中(从宏录制器):

代码语言:javascript
复制
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表格中,但越南文字不再正确显示。

我也尝试过使用Data选项卡,但Excel似乎忽略了编码:

代码语言:javascript
复制
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; ‰; ™,™;

EN

回答 1

Stack Overflow用户

发布于 2015-09-08 07:01:11

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

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

代码语言:javascript
复制
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")
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23626622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档