将 CSV 数据从 Web 服务导入 Excel

发布于 2024-12-08 19:15:21 字数 319 浏览 0 评论 0原文

我编写了一个简单的 Web 服务,可返回大量 csv 数据。我将使用 Excel 的“来自 Web 的数据”功能将其以表格格式导入到 Excel 中。

有没有办法让 Excel 自动解析作为导入操作的一部分返回到各个列中的 csv 字段?

目前,我执行此操作的唯一方法是首先将数据导入到单个列中,然后编写 VBA 代码来选择数据并使用 TextToColumns 拆分数据。这感觉很混乱/容易出错。

我的另一个选择是修改 Web 服务器以将数据作为 HTML 提供。但是,我不愿意这样做,因为在每个 csv 字段周围添加标签将极大地影响返回的数据量。

I have written a simple web service that returns large volumes of csv data. I will to import this into Excel in a tabular format using Excel's "Data From Web" function.

Is there a way to get Excel to automatically parse the csv fields returned into individual columns as part of the import operation?

At present the only means I have for doing this is to first import the data into a single column and then write VBA code to select the data and split it using TextToColumns. This feels messy / error-prone.

The other alternative I have is to modify the web server to serve back the data as HTML. However, I'm reluctant to do this as adding tags around each csv field will greatly impact the volume of data returned.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宛菡 2024-12-15 19:15:21

Adamski,

这是我用的东西。我在互联网上找到了核心,但不知道在哪里。

它的作用是打开一个制表符分隔的文件并读取 Excel 工作表中的数据

If Answer1 = vbYes Then    'I asked prior if to import a tab separated file
    Sheets("ZHRNL111").Select    'Select the sheet to dump the data
    On Error Resume Next
    With ActiveSheet
        If .AutoFilterMode Then .ShowAllData    'undo any autofilters
    End With
    Sheets("ZHRNL111").Cells.Clear    'remove any previous data
    On Error GoTo 0
    Range("A1").CurrentRegion.Delete
    Fname = MyPath & "\LatestReports\Report-111.tsv"
    Open Fname For Input As #1
    iRow = 1
    Line Input #1, Record
    On Error Resume Next
    Do Until EOF(1)
        P = Split(Record, vbTab)
        For iCol = 1 To 14
            Cells(iRow, iCol) = P(iCol - 1)
        Next iCol
        iRow = iRow + 1
        Line Input #1, Record
    Loop
    On Error GoTo 0
    Close 1
End If

问候,

Robert Ilbrink

Adamski,

Here is something that I use. I found the core somewhere on the internet, but don't know where.

What it does is it opens a tab separated file and reads the data in an excel sheet

If Answer1 = vbYes Then    'I asked prior if to import a tab separated file
    Sheets("ZHRNL111").Select    'Select the sheet to dump the data
    On Error Resume Next
    With ActiveSheet
        If .AutoFilterMode Then .ShowAllData    'undo any autofilters
    End With
    Sheets("ZHRNL111").Cells.Clear    'remove any previous data
    On Error GoTo 0
    Range("A1").CurrentRegion.Delete
    Fname = MyPath & "\LatestReports\Report-111.tsv"
    Open Fname For Input As #1
    iRow = 1
    Line Input #1, Record
    On Error Resume Next
    Do Until EOF(1)
        P = Split(Record, vbTab)
        For iCol = 1 To 14
            Cells(iRow, iCol) = P(iCol - 1)
        Next iCol
        iRow = iRow + 1
        Line Input #1, Record
    Loop
    On Error GoTo 0
    Close 1
End If

Regards,

Robert Ilbrink

☆獨立☆ 2024-12-15 19:15:21

根据您运行的 Excel 版本,您应该能够在 Excel 中打开 .csv 并使用 Excel 内置的文本到列功能。

另外,如果您可以修改 csv 以根据逗号“,”而不是制表符拆分列,excel 将直接打开它,而无需对其进行格式化。
然而,我知道这有时可能会成为一个问题,具体取决于您导入的数据,因为如果数据包含逗号,它必须在引号内。
根据我的经验,最好的方法是如果可能的话在每个字段上使用引号。

希望这有帮助。

Depending on the version of excel you are running you should be able to open the .csv in excel and use the text to columns feature built into excel.

Also, if you could modify your csv to split columns based on commas "," instead of tabs excel would open it directly without the need to format it.
I know however this can sometimes be a problem depending on the data you are importing because if the data contains a comma it must be inside quotations.
In my experience the best way is to use quotations on every field if possible.

Hope this helps.

后eg是否自 2024-12-15 19:15:21

实际上,我现在正在创建一个产品,以在 Excel 的 XML 和 JSON 中执行此操作。我知道逗号分隔在 Excel 中确实有效,但有一些注意事项。解决此问题的一种方法是在“来自 Web 的数据”功能的分隔符之间的文本周围放置一些“”。然而,这仍然存在问题。我确实发现,尽管 XML 的大小有所增加,但它是快速周转的最佳选择。我能够创建该服务并将 Excel 文档交给我的项目经理,他可以随时更新该文档。

I am actually creating a product right now to do this in both XML and JSON for Excel. I know comma delimited does work in Excel, with some caveats. One way around it is to put some "" around the text in between the delimiters for the "Data From Web" feature. There are still issues with that however. I did find that despite it's increased size, XML was the best option for quick turn around. I was able to create the service and hand my project manager the Excel document which he could update at anytime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文