提高 MS Excel 写入性能
我在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以读取整个范围并将其保存到二维数组中,而不是逐个单元地读取。然后,您可以像访问 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
伟大的!!!
我使用了 2D 数组方法并实现了巨大的性能提升!
以前,我使用了逐个单元格的方法,如下所示,
我曾经迭代一系列单元格并用于复制每个单元格中的值。
这里每个
sheet.Cells
和cell.Value
都是一个互操作调用,并且对于每个调用,它都会调用 Excel.exe,这会花费更多时间。在 2D 方法中,我已将要复制到 Excel 单元格中的数据填充到 2D 数组中,然后将 2D 数组分配给所选单元格范围的值。如下所示,
经过这些修改,我收集了两种方法的性能数据,结果非常好! 后一种方法的速度是前一种方法的 25 倍。
同样,我使用二维数组方法从单元读取数据,并看到了类似的性能提升。代码示例如下所示。
逐个单元方法、
2D 阵列方法,
请参考,
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,
I used to iterate over a range of cells and used to copy the value in each cell.
Here every
sheet.Cells
andcell.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,
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,
2D array approach,
Please refer,
http://support.microsoft.com/kb/306023 ,
http://dotnetperls.com/excel-interop (thanks ChickSentMeHighE for the link)
Enjoy the performance!!!