Excel VBA importing CSV from a web service with post data

3.3k views Asked by At

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 - i.e. extract the data from a web service, using post data, but also ensure excel correctly interprets the comma delimited format?

2

There are 2 answers

1
jas_raj_total On

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 - i.e. use Connection:"URL".

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

0
Fire Ruler On

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.