How to parse line by line WinHTTP response: UTF-8 encoded CSV?

后端 未结 2 1357
星月不相逢
星月不相逢 2020-12-06 22:11

As the next step for my happily solved problem (Not understanding why WinHTTP does NOT authenticate certain HTTPS resource) I need to prettily parse obtained CSV. At the mom

相关标签:
2条回答
  • 2020-12-06 23:08

    Finally I found both solutions on my own:

    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
    

    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.

    0 讨论(0)
  • 2020-12-06 23:11

    This line

    OutputRange = WorksheetFunction.Transpose(CSV_Strings)
    

    should be like this

    OutputRange.Formula = WorksheetFunction.Transpose(CSV_Strings)
    
    0 讨论(0)
提交回复
热议问题