本文介绍了C#ExcelPackage(EPPlus)DeleteRow不会更改图纸尺寸吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个数据导入工具,该工具可以接受用户的EXCEL文件并解析该文件中的数据以将数据导入到我的应用程序中。

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.

I正在运行DeleteRow的一个奇怪问题,尽管似乎以前有人会遇到此问题,但我似乎无法在线找到任何信息。如果这是一个重复的问题,我深表歉意,但是在网上搜索后,除了仍然无法解决我的问题。

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. 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.

问题:

我使用以下代码尝试通过ExcelPackage删除具有空白数据的任何行。

I use the following code to attempt to "remove" any row that has blank data through 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
                }

逐步执行该代码,我可以看到确实需要正确的行号调用DeleteRow,但是问题是当我去设置返回的结果对象的文件中的总行数时:

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:

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

ws.Dimension.End.Row仍将返回原始行数,即使在调用DeleteRow之后也是如此。

ws.Dimension.End.Row will still return the original row count even after the calls to DeleteRow.

我的问题是...我是否必须保存工作表或调用某些东西才能使工作表意识到那些行已被删除?如果行仍然存在,调用 DeleteRow有什么意义?任何对此的见识将不胜感激...

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"? Any insight on this would be greatly appreciated...

谢谢

推荐答案

I think I figured out the problem. This is yet again another closure issue in C#. The problem is that the reference to "ws" is still the same reference from before the DeleteRow call.

为了获得更新的维度,您必须重新声明工作表,例如:

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.

这篇关于C#ExcelPackage(EPPlus)DeleteRow不会更改图纸尺寸吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!