2

为此,我编写了以下 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,我看到三个选项,然后是成功的 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

它缺少基本的身份验证标头,因此由

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 实际上正在工作并使用基本身份验证。

如何使用带有基本身份验证的 https 将 CSV 数据加载到 Excel 2013 中?

4

0 回答 0