gpt4 book ai didi

vba - 将 ADODB 二进制流转换为字符串 vba

转载 作者:行者123 更新时间:2023-12-04 20:25:29 24 4
gpt4 key购买 nike

我有以下问题:
我有一个存储在服务器上的 CSV 文件,但它有 3 个字符作为分隔符:“[|]”。我想从 URL 加载数据并使用 [|] 作为分隔符将数据填充到我的 Excel 页面的列中。到目前为止,我找到了使用 ADODB 记录集从网站加载文件的代码,但我无法进一步了解:

 myURL = "http://www.example.com/file.csv"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "username", "password"
WinHttpReq.send

myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1 'binary type
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile "E:\file.csv", 2 ' 1 = no overwrite, 2 = overwrite
oStream.Close
End If

这可以很好地直接保存文件。但我不想将它保存到文件中,我想在适当的单元格中输入数据。有没有办法做到这一点?我不想你使用 Internet Explorer 对象

最佳答案

使用常规 csv 文件测试正常:

Sub Tester()
Dim myURL As String, txt As String, arrLines, arrVals
Dim l As Long, v As Long, WinHttpReq As Object
Dim rngStart As Range

myURL = "http://www.mywebsite.com/file.csv"

Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False, "username", "password"
WinHttpReq.send

txt = WinHttpReq.responseText

'might need to adjust vbLf >> vbCrLf or vbCr
' depending on the file origin (Win/Unix/Mac)
arrLines = Split(txt, vbLf)

Set rngStart = ActiveSheet.Range("A1")

For l = 0 To UBound(arrLines)
arrVals = Split(arrLines(l), "[|]")
For v = 0 To UBound(arrVals)
rngStart.Offset(l, v).Value = arrVals(v)
Next v
Next l

End Sub

关于vba - 将 ADODB 二进制流转换为字符串 vba,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com