VBA从Web服务器导入UTF-8 CSV文件 [英] VBA importing UTF-8 CSV file from a web server
问题描述
我在Web服务器上存储了一个UTF-8 CSV文件.当我下载文件时,将其放在硬盘驱动器上,然后使用宏(从宏记录器)将其导入Excel工作表中:
I have a UTF-8 CSV file stored on a web server. When I download the file put it on my hard drive and I then import it into an Excel sheet with this macro (from the macro recorder) :
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
所有字符(越南字符)均正确显示.
All of the characters (vietnamese characters) are displayed correctly.
当我尝试相同的宏但未提供文件的本地地址("C:/myFile.csv")时,我传递了文件的URL("http://myserver.com/myFile.csv ),CSV已正确导入到我的Excel工作表中,但是越南语字符不再正确显示.
When I try the same macro but instead of giving the local address of the file ("C:/myFile.csv") I pass the URL of the file ("http://myserver.com/myFile.csv") the CSV is correctly imported into my Excel sheet but the vietnamese characters are not displayed correctly anymore.
我也尝试过使用数据"标签,但是Excel似乎忽略了该编码:
I have also tried using the Data tab but the encoding seems be ignored by 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; ‰; ™,™;
which Excel reads wrongly as: „; Â; ˜; Â1/4; ‰; ™,™;
推荐答案
我一直在研究一个类似的问题,我们将utf-8编码的csv文件导入到工作表中.我不是从Web服务器中提取数据,但这可能会有所帮助.
I have been looking at a similar problem where we import utf-8 encoded csv files in to a worksheet. I am not pulling the data from a web server but this might help.
我的解决方案是将utf-8文件读取为局部变量,然后将其插入工作表中.我尝试将数据保存到使用ansi编码的临时文件中,但是这样做会使所有字符失去重音符号.
My solution is to read the utf-8 file to a local variable then insert it into a sheet. I tried saving the data to a temp file with ansi encoding but doing this caused all the characters to lose their accents.
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")
这篇关于VBA从Web服务器导入UTF-8 CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!