提高 MS Excel 写入性能

发布于 2024-09-13 08:20:18 字数 164 浏览 0 评论 0原文

我在从 MS-Excel 单元格读取/写入数据时遇到性能问题。我正在使用 MS Excel 11.0 对象库通过 VB.NET 实现自动化。

目前,读取和写入 Excel 文件需要花费太多时间。 (10分钟读取1000行:( )。看来逐个单元读写的效率不是那么高。有没有办法使用批量操作来读写数据?

I am facing performance issues while reading/writing data from/to MS-Excel cells. I am using MS Excel 11.0 object library for automation with VB.NET.

Currently it takes too much time to read and write from/to Excel files. (10 mins to read 1000 rows :( ). It seems the cell-by-cell reading and writing approach is not that effiecient. Is there any way to read/write data using bulk operation?

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

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

发布评论

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

评论(2

甜扑 2024-09-20 08:20:18

您可以读取整个范围并将其保存到二维数组中,而不是逐个单元地读取。然后,您可以像访问 Excel 中的单元格一样访问二维数组。

我不太熟悉 VB.NET 的 excel 对象,但如果您了解 C#,请快速阅读此链接并尝试实现它。

http://dotnetperls.com/excel-interop
阅读“获取工作簿数据”部分

Rather than reading cell by cell you could read a whole range and save it into a 2D arrray. You can then access the 2D array as you would access a cell in excel.

I'm not well versed in VB.NET for excel objects but if you understand C# then give this link a quick read and try to implement it.

http://dotnetperls.com/excel-interop
Read the "Getting workbook data" section

德意的啸 2024-09-20 08:20:18

伟大的!!!

我使用了 2D 数组方法并实现了巨大的性能提升!

以前,我使用了逐个单元格的方法,如下所示,

Dim cell As Excel.Range = Nothing
cell = sheet.Cells(rowIndex, colIndex)
cell.Value = "Some value"

我曾经迭代一系列单元格并用于复制每个单元格中的值。
这里每个 sheet.Cellscell.Value 都是一个互操作调用,并且对于每个调用,它都会调用 Excel.exe,这会花费更多时间。

在 2D 方法中,我已将要复制到 Excel 单元格中的数据填充到 2D 数组中,然后将 2D 数组分配给所选单元格范围的值。如下所示,

Dim darray(recordCount - 1, noOfCol - 1) As String
//Fill the data in darray
//startPosRange = Get the range of cell from where to start writing data
startPosRange = startPosRange.Resize(recordCount, noOfCol)
startPosRange.Value = darray

经过这些修改,我收集了两种方法的性能数据,结果非常好! 后一种方法的速度是前一种方法的 25 倍

同样,我使用二维数组方法从单元读取数据,并看到了类似的性能提升。代码示例如下所示。

逐个单元方法、

Dim usedRange As Excel.Range = sheet.UsedRange
For Each row As Excel.Range In usedRange.Rows()
For Each cellData As Excel.Range In row.Cells
    //Gather cellData.Value in some container.
Next

2D 阵列方法,

Dim usedRange As Excel.Range = sheet.UsedRange
//Here the array index starts from 1. why???
Dim darray(,) As Object = CType(usedRange.Value, Object(,))

Dim rows As Integer = darray.GetUpperBound(0)
Dim cols As Integer = darray.GetUpperBound(1)
For i As Integer = 1 To rows    
    For j As Integer = 1 To cols
        Dim str As String
        If darray(i, j) Is Nothing Then
            str = ""
        Else
            str = darray(i, j).ToString
        End If
        //Use value of str
    Next
Next

请参考,
http://support.microsoft.com/kb/306023 ,
http://dotnetperls.com/excel-interop(感谢 ChickSentMeHighE 提供的链接)

享受性能! !

Great!!!

I used the 2D array approach and achieved the enormous performance boost!!.

Previously I used the cell-by-cell aprroach as shown below,

Dim cell As Excel.Range = Nothing
cell = sheet.Cells(rowIndex, colIndex)
cell.Value = "Some value"

I used to iterate over a range of cells and used to copy the value in each cell.
Here every sheet.Cells and cell.Value is an interop call and for every call it gives call to the Excel.exe, which costs more time.

In 2D approach I have filled the data, which is to be copied in Excel cells, in 2D array and then assigned the 2D array to the value of the selected reange of cells. It is as shown below,

Dim darray(recordCount - 1, noOfCol - 1) As String
//Fill the data in darray
//startPosRange = Get the range of cell from where to start writing data
startPosRange = startPosRange.Resize(recordCount, noOfCol)
startPosRange.Value = darray

After these modifications, I gathered the performance data for both the approaches and results are surprisingly great!!. The later approach is 25 times as fast as previous one.

Similarly, I have used the 2D array approach for reading data from cells and seen the similar performance boost. Code samples are as shown below.

Cell-by-cell approach,

Dim usedRange As Excel.Range = sheet.UsedRange
For Each row As Excel.Range In usedRange.Rows()
For Each cellData As Excel.Range In row.Cells
    //Gather cellData.Value in some container.
Next

2D array approach,

Dim usedRange As Excel.Range = sheet.UsedRange
//Here the array index starts from 1. why???
Dim darray(,) As Object = CType(usedRange.Value, Object(,))

Dim rows As Integer = darray.GetUpperBound(0)
Dim cols As Integer = darray.GetUpperBound(1)
For i As Integer = 1 To rows    
    For j As Integer = 1 To cols
        Dim str As String
        If darray(i, j) Is Nothing Then
            str = ""
        Else
            str = darray(i, j).ToString
        End If
        //Use value of str
    Next
Next

Please refer,
http://support.microsoft.com/kb/306023 ,
http://dotnetperls.com/excel-interop (thanks ChickSentMeHighE for the link)

Enjoy the performance!!!

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