我在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表格中,但越南文字不再正确显示。
我也尝试过使用Data选项卡,但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; ‰; ™,™;
发布于 2015-09-08 07:01:11
我一直在研究一个类似的问题,我们将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")
https://stackoverflow.com/questions/23626622
复制相似问题