简体   繁体   English

C#ExcelPackage(EPPlus)DeleteRow不会更改工作表尺寸?

[英]C# ExcelPackage (EPPlus) DeleteRow does not change sheet dimension?

I am trying to build a data import tool that accepts an EXCEL file from the user and parses the data from the file to import data into my application. 我正在尝试构建一个数据导入工具,它接受来自用户的EXCEL文件并解析文件中的数据以将数据导入我的应用程序。

I am running across a strange issue with DeleteRow that I cannot seem to find any information online, although it seems like someone would have come across this issue before. 我在使用DeleteRow遇到一个奇怪的问题,我似乎无法在网上找到任何信息,尽管看起来有人会遇到过这个问题。 If this is a duplicate question, I apologize, however I could not find anything related to my issue after searching the web, except for this one which still isn't solving my problem. 如果这是一个重复的问题,我道歉,但是在搜索网页后我找不到与我的问题相关的任何内容,除了这个仍然没有解决我的问题。

So the issue: 所以问题:

I use the following code to attempt to "remove" any row that has blank data through ExcelPackage. 我使用以下代码尝试通过ExcelPackage“删除”具有空白数据的任何行。

                for (int rowNum = 1; rowNum <= ws.Dimension.End.Row; rowNum++)
                {
                    var rowCells = from cell in ws.Cells
                                        where (cell.Start.Row == rowNum)
                                        select cell;
                    if (rowCells.Any(cell => cell.Value != null))
                    {
                        nonEmptyRowsInFile += 1;
                        continue;
                    }
                    else ws.DeleteRow(rowNum);
                    //Update: ws.DeleteRow(rowNum, 1, true) also does not affect dimension
                }

Stepping through that code, I can see that the DeleteRow is indeed getting called for the proper row numbers, but the issue is when I go to set the "total rows in file" count on the returned result object: 单步执行该代码,我可以看到DeleteRow确实被调用了正确的行号,但问题是当我在返回的结果对象上设置“文件中的总行数”时:

parseResult.RowsFoundInFile = (ws.Dimension.End.Row);

ws.Dimension.End.Row will still return the original row count even after the calls to DeleteRow. 即使在调用DeleteRow之后,ws.Dimension.End.Row仍将返回原始行计数。

My question is...do I have to "save" the worksheet or call something in order for the worksheet to realize that those rows have been removed? 我的问题是......我是否必须“保存”工作表或调用某些东西才能让工作表意识到这些行已被删除? What is the point of calling "DeleteRow" if the row still "exists"? 如果行仍然“存在”,调用“DeleteRow”有什么意义? Any insight on this would be greatly appreciated... 任何有关这方面的见解将不胜感激......

Thanks 谢谢

I think I figured out the problem. 我想我弄明白了这个问题。 This is yet again another closure issue in C#. 这又是C#中的另一个关闭问题。 The problem is that the reference to "ws" is still the same reference from before the DeleteRow call. 问题是对“ws”的引用仍然是DeleteRow调用之前的引用。

In order to get the "updated" dimension, you have to redeclare the worksheet, for example: 为了获得“更新”维度,您必须重新声明工作表,例如:

 ws = excelPackage.Workbook.Worksheets.First();

Once you get a new reference to the worksheet, it will have the updated dimensions, including any removed/added rows/columns. 获得对工作表的新引用后,它将具有更新的维度,包括任何已删除/添加的行/列。

Hopefully this helps someone. 希望这有助于某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM