Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

So the issue:

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
                }

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 will still return the original row count even after the calls to 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...

Thanks

like image 459
Josh Avatar asked Feb 09 '16 16:02

Josh


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

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.

like image 142
Josh Avatar answered Oct 03 '22 20:10

Josh