且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何逐行解析WinHTTP响应:UTF-8编码的CSV?

更新时间:2023-11-27 11:49:04

最后,我自己找到了两个解决方案:

Finally I found both solutions on my own:


  1. 借助ADODB.Stream将CSV转换为UTF-8转换(详见: http://www.motobit.com/tips/detpg_binarytostring/

  2. 使用文本拆分CSV和进一步解析字符串数组到数据 Excel例程

  1. CSV to UTF-8 conversion with the help of ADODB.Stream (see for more: http://www.motobit.com/tips/detpg_binarytostring/)
  2. Splitting CSV and further parsing of strings array using Text to Data Excel routine

以下是代码的相关部分:

Below is the related part of code:

'CSV to UTF-8
Set FileStream = CreateObject("ADODB.Stream")
FileStream.Open
FileStream.Type = 1 'Binary
FileStream.Write HTTPReq.responseBody
FileStream.Position = 0
FileStream.Type = 2 'Text
FileStream.Charset = "UTF-8"
CSV_Text = FileStream.ReadText
FileStream.Close
'CSV Splitting
CSV_Strings = Split(Trim(CSV_Text), vbLf)
ThisWorkbook.Worksheets("RM_Log").Cells.ClearContents
Set OutputRange = ThisWorkbook.Sheets("RM_Log").Range("A1:A" & UBound(CSV_Strings) + 1)
OutputRange = WorksheetFunction.Transpose(CSV_Strings)
OutputRange.TextToColumns Destination:=ThisWorkbook.Sheets("RM_Log").Range("A1"), _
    DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
    Tab:=False, Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 3), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
    Array(7, 1), Array(8, 1), Array(9, 1)), DecimalSeparator:=".", _
    TrailingMinusNumbers:=True

因此,我的Excel文件现在完全自给自足。希望这将帮助别人以及。非常感谢所有留下评论的人 - 他们缩小了我的搜索范围。

As a result, my Excel file is now totally self-sufficient. Hope this will help someone else as well. Many thanks to everyone who left comments - they narrowed my search.