为此,我编写了以下VBA脚本并将其绑定到一个设置一些参数并加载数据的按钮。之后它会删除连接以避免一堆陈旧的连接。
Private Sub LoadData_Click()
Dim ConnString As String
Dim URL As String
URL = "https://some-server/some/path/some-file.csv"
ConnString = "TEXT;" + URL + "?partner_code=" + CStr(Range("B2")) + "&from=" + CStr(Range("B3")) + "&to=" + CStr(Range("B4"))
Sheets("Import").Select
Sheets("Import").UsedRange.Clear
ActiveSheet.Range("A1").Value = "Data is being loaded. Hold on..."
With ActiveSheet.QueryTables.Add(Connection:=ConnString, Destination:=ActiveSheet.Range("A1"))
.Name = _
" "
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlOverwriteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = xlWindows
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileDecimalSeparator = ","
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Do While ActiveWorkbook.Connections.Count > 0
ActiveWorkbook.Connections.Item(ActiveWorkbook.Connections.Count).Delete
Loop
Sheets("Parameter").Select
End Sub
它适用于Excel 2010,但不适用于2013.单击我的按钮时,我提示输入凭据并输入它们。使用Excel 2010,我在服务器端看到apache日志中的一些多余的OPTIONS请求和一个HEAD,但最后只有一个GET来提取数据。
使用Excel 2013,我看到三个OPTIONS,然后是一个成功的HEAD(200),最后是一个不成功的GET(401)。为了进一步研究它,我确实激活了mod_dumpio模块以查看发生了什么。 Apache当然有权拒绝excel 2013:
HEAD /some/path/some-file.csv?partner_code=25010&from=01.01.2014&to=01.06.2014 HTTP/1.1\r\n
...
Authorization: Basic Y29udHJvbGxpbmc6Rm9vNWFoUGg=\r\n
以200响应,然后直接出现GET
GET /some/path/some-file.csv?partner_code=25010&from=01.01.2014&to=01.06.2014 HTTP/1.1\r\n
缺少基本的auth标头,因此由
回答HTTP/1.1 401 Unauthorized\r\nDate: Thu, 03 Jul 2014 18:18:59 GMT\r\nServer: Apache\r\nPragma: No-cache\r\nCache-Control: no-cache\r\nExpires: Thu, 01 Jan 1970 01:00:00 CET\r\nWWW-Authenticate: Basic realm="Import/Export"\r\nVary: Accept-Encoding\r\nContent-Encoding: gzip\r\nContent-Length: 378\r\nConnection: close\r\nContent-Type: text/html;charset=utf-8\r\n\r\n
我一步一步地遵循http://support.microsoft.com/kb/2123563/en-us中的说明,但没有帮助。此外,HEAD实际上正在使用Basic auth。
如何使用带基本身份验证的https将CSV数据加载到Excel 2013中?