简体   繁体   English

从URL逗号分隔的Excel QueryTables.Add

[英]Excel QueryTables.Add from URL Comma Delimited

I have a server that returns large amounts of comma separated data in an http response. 我有一台服务器,它在http响应中返回大量逗号分隔的数据。 I need to import this data into excel. 我需要将此数据导入excel。

I have this working by passing the contents to a temp file and then reading the temp file as a csv, but this process seems inefficient. 我通过将内容传递到临时文件,然后以CSV格式读取临时文件来完成此工作,但是此过程效率低下。 The query tables can read directly from the http response, but it puts each line of data into a single cell, rather than separating into one cell per comma. 查询表可以直接从http响应中读取,但是会将每行数据放入单个单元格中,而不是每个逗号分隔成一个单元格。

Is it possible to read comma separated data from an http response directly into excel from a C# excel add-in? 是否可以通过C#Excel加载项将HTTP响应中的逗号分隔数据直接读入excel?

Thanks! 谢谢!

    public static void URLtoCSV(string URL, Excel.Worksheet destinationSheet, Excel.Range     destinationRange, int[] columnDataTypes, bool autoFitColumns)
    {
        destinationSheet.QueryTables.Add(
            "URL;" + URL,
        destinationRange, Type.Missing);
        destinationSheet.QueryTables[1].Name = URL;
        destinationSheet.QueryTables[1].FieldNames = true;
        destinationSheet.QueryTables[1].RowNumbers = false;
        destinationSheet.QueryTables[1].FillAdjacentFormulas = false;
        destinationSheet.QueryTables[1].PreserveFormatting = true;
        destinationSheet.QueryTables[1].RefreshOnFileOpen = false;
        destinationSheet.QueryTables[1].RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells;
        destinationSheet.QueryTables[1].SavePassword = false;
        destinationSheet.QueryTables[1].SaveData = true;
        destinationSheet.QueryTables[1].AdjustColumnWidth = true;
        destinationSheet.QueryTables[1].RefreshPeriod = 0;
        destinationSheet.QueryTables[1].Refresh(false);

        if (autoFitColumns == true)
            destinationSheet.QueryTables[1].Destination.EntireColumn.AutoFit();

    }

The easier solution than the one you reference is to use the type of "TEXT" instead of URL. 比您引用的解决方案更简单的解决方案是使用“ TEXT”类型而不是URL。 TEXT supports all CSV imports, including from HTTP sources. TEXT支持所有CSV导入,包括从HTTP来源的导入。 URL appears to be designed to handle screen scraping more than anything else. URL似乎被设计用来处理屏幕抓取,而不是其他任何事情。

eg in your case: 例如,在您的情况下:

destinationSheet.QueryTables.Add("URL;" + URL,

becomes 变成

destinationSheet.QueryTables.Add("TEXT;" + URL,

And for those stumbling upon this post asking the same question but with VB scripting in Excel, the complete solution would look like: 对于那些绊脚石问同样的问题,但在Excel中使用VB脚本的用户,完整的解决方案如下所示:

' Load new data from web
With ActiveSheet.QueryTables.Add(Connection:="TEXT;http://yourdomain.com/csv.php", Destination:=Range("$A$1"))
.TextFileCommaDelimiter = True
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlOverwriteCells
.SavePassword = False
.SaveData = False
.AdjustColumnWidth = True
.Refresh BackgroundQuery:=False
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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