简体   繁体   中英

Excel VBA importing CSV from a web service with post data

I have a web service that produces a large amount of CSV data, which I need to import into excel 2013.

I have found the straightforward way to do this:

With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & URL, Destination:=Cells(1, 1))
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlOverwriteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .WebPreFormattedTextToColumns = True
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 850
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
    .WorkbookConnection.Delete
End With

However, I also need to send the web service a payload with parameters in order to filter the data it returns.

The only way I have found of doing this is to use the .PostText property, but this requires the connection to be "URL;" rather than "TEXT;" and therefore doesn't allow the .TextFileCommaDelimiter property, which is critical to the output in the worksheet.

Is there a straightforward way to solve this - ie extract the data from a web service, using post data, but also ensure excel correctly interprets the comma delimited format?

Are you able to change the format provided by the web service?

I have returned data in HTML form ( <table>...</table> ) to then do what you need - ie use Connection:"URL" .

The data is then auto-formatted based on the HTML table tags into an Excel sheet.

Had similar problem, and XML variant was taking too long to deserialize in Excel 2007 (35k rows).

So, since it is your service, you can implement both POST and GET http options, then encode the parameters for a GET request by adding ?param=value to the url, eg: http://yourhost/service.do?AsOfDate=20140903 , still using "TEXT;" in connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM