在 vb.net 中循环遍历 Excel 工作簿

发布于 2024-11-25 19:31:41 字数 155 浏览 1 评论 0原文

我正在使用一个包含 xls 文件的文件夹,这些文件的格式均相同(通过在定价应用程序中输入数字自动生成)。我需要提取每个文件中同名工作表上单元格 D54 中的数据。似乎无法让任何东西起作用来使其循环。

有什么想法可以做到这一点吗?

I am working with a folder of xls files that are all in identical format (automatically generated by entering numbers into a pricing app). I need to pull the data that is in cell D54 on the worksheet of the same name in every file. Can't seem to get anything to work to make it loop.

Any ideas how this can be done?

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

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

发布评论

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

评论(1

青巷忧颜 2024-12-02 19:31:42

如果您可以生成 XLSX 格式的文件,那么这就是我要做的。

http://epplus.codeplex.com/

这是一个令人惊叹的组件库,用于处理 Excel XLSX 工作表。

示例...

Sub temp()
    Dim out As New List(Of String)
    Using pac As New ExcelPackage(New IO.FileInfo("c:\temp.xlsx"))
        For Each wb As ExcelWorksheet In pac.Workbook.Worksheets
            out.Add(wb.Cells("D54").Value.ToString)
        Next
    End Using
End Sub

否则,选项是从 Office 引用 Excel Com+ 库来打开一个 XLS 工作表,其代码应与下面类似。

Sub temp2()
    Dim out As New List(Of String)
    Dim app As New Microsoft.Office.Interop.Excel.Application
    app.DisplayAlerts = False
    Dim wb As Microsoft.Office.Interop.Excel.Workbook = app.Workbooks.Open("c:\temp.xls")
    For Each ws As Microsoft.Office.Interop.Excel.Worksheet In wb.Worksheets
        Dim r As Microsoft.Office.Interop.Excel.Range = ws.Cells(54, 4)
        out.Add(r.Value.ToString)
    Next
    app.close()
End Sub

If you can have the files generated in XLSX format then this is what I would do.

http://epplus.codeplex.com/

This is an amazing component library for dealing with Excel XLSX sheets.

Example...

Sub temp()
    Dim out As New List(Of String)
    Using pac As New ExcelPackage(New IO.FileInfo("c:\temp.xlsx"))
        For Each wb As ExcelWorksheet In pac.Workbook.Worksheets
            out.Add(wb.Cells("D54").Value.ToString)
        Next
    End Using
End Sub

Otherwise the option is to reference the Excel Com+ Library from office to open an XLS sheet with what should be similar code as below.

Sub temp2()
    Dim out As New List(Of String)
    Dim app As New Microsoft.Office.Interop.Excel.Application
    app.DisplayAlerts = False
    Dim wb As Microsoft.Office.Interop.Excel.Workbook = app.Workbooks.Open("c:\temp.xls")
    For Each ws As Microsoft.Office.Interop.Excel.Worksheet In wb.Worksheets
        Dim r As Microsoft.Office.Interop.Excel.Range = ws.Cells(54, 4)
        out.Add(r.Value.ToString)
    Next
    app.close()
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文