Open XML SDK v2.0 删除 20,000 行中的第一行时出现性能问题行 Excel 文件

发布于 2024-08-14 15:42:40 字数 273 浏览 7 评论 0原文

使用 OpenXML SDK v2.0 删除 20,000 多行 Excel 文件中的第一行时,有人遇到性能问题吗?

我正在使用 Open XML SDK 文档中建议的删除行编码。使用 Open XML SDK 删除第一行需要几分钟的时间,但在 Excel 应用程序中只需要一秒钟。

我最终发现瓶颈实际上是处理行删除时的冒泡方法。删除行后有许多行更新。因此,就我而言,大约有 20,000 行需要更新,逐行上移数据。

我想知道是否有更快的方法来删除行。

有人有想法吗?

Do anyone come across a performance issue when deleting a first row in a 20,000+ rows Excel file using OpenXML SDK v2.0?

I am using the delete row coding suggested in the Open XML SDK document. It takes me several minutes just to delete the first row using Open XML SDK, but it only takes just a second in Excel applicaton.

I eventually found out that the bottle-neck is actually on the bubble-up approach in dealing with row deletion. There are many rows updating after the deleted row. So in my case, there are around 20,000 rows to be updated, shifting up the data row by row.

I wonder if there is any faster way to do the row deletion.

Do anybody have an idea?

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

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

发布评论

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

评论(2

白日梦 2024-08-21 15:42:40

好吧,坏消息是:是的,就是这样

将 SDK 本身移到 System.IO.Packaging 中并在 Linq 中创建一个 IEnumerable/List 可能会获得更好的性能所有行的 to-XML,将其复制到新的 IEnumerable/List(不含第一行),重写 r 属性 是它在索引中的位置,并将其写回到 内覆盖现有的子项。

您需要对 sharedStrings.xml 文件中的任何字符串执行相同的操作 - 即删除其中的 . 元素被删除的行,但在这种情况下,它们现在已隐式索引,因此您可以直接删除它们。

Well, the bad news here is: yep, that's the way it is.

You may get slightly better performance moving outside of the SDK itself into System.IO.Packaging and just creating an IEnumerable/List in like Linq-to-XML of all the rows, copy that to a new IEnumerable/List without the first row, rewrite the r attribute of <row r="?"/> to be it's place in the index, and the write that back inside <sheetData/> over existing children.

You'd need to kind of do the same for any strings in the sharedStrings.xml file - i.e. removing the <ssi>.<si> elements that were in the row that was deleted, but in this case they are now implicitly indexed, so you'd be able to get away with just outright removing them.

情独悲 2024-08-21 15:42:40

解压缩文件、对其进行操作并重新打包的方法非常容易出错。

怎么样:如果您说它在 Excel 中运行良好:您尝试过使用 Interop 吗?这将启动一个新的 Excel 实例(可见或不可见),然后您可以打开文件、删除该行、保存并再次关闭应用程序。

using System;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
public void OpenAndCloseExcel() 
{
    Excel.Application excelApp = new Excel.Application();
    // Open Workbook, open Worksheet, delete line, Save
    excelApp.Quit();
}

Range 对象适用于多种用途。也用于删除元素。看一下:MSDN 范围描述< /a>.还有一个提示:Interop 使用 Excel,因此所有对象都必须使用从 1 开始的索引来寻址!
如需更多资源,请查看
此 StackOverflow-thread

The approach of unzipping the file, manipulating it and repacking it is very error-prune.

How about this: If you say, that it works fine in Excel: Have you tried to use the Interop? This starts a new instance of Excel (either visible or invisible), then you can open the File, delete the line, save and close the application again.

using System;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
public void OpenAndCloseExcel() 
{
    Excel.Application excelApp = new Excel.Application();
    // Open Workbook, open Worksheet, delete line, Save
    excelApp.Quit();
}

The Range-object is qualified for many purposes. Also for deleting elements. Have a look at: MSDN Range-Description. One more hint: Interop uses Excel, so all Objects have to be adressed with a 1-based index!
For more resources take a look at this StackOverflow-thread.

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