Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# EPPlus delete row from excel sheet

Tags:

c#

excel

epplus

I'm currently working with an Excel file that has leading rows that have information I don't need. These extra rows also mess with importing that data in the header row below. So I'm trying to remove them to work with the data.

using (var pack = new ExcelPackage(myFileInfo))
{
    // Should return the sheet name
    var ws = pack.Workbook.Worksheets.FirstOrDefault();
    // Should Delete rows 1-5 and shift up the rows after deletion
    ws.DeleteRow(1,5,true);
}

I was thinking something like the above would work, but I've not had much success with it.

The goal would be to delete rows 1-5, shift up the rest of the data (maybe a merge would work?) then convert it into a datatable.

Anyone have tips tips or resources on removing rows from my excel sheet (prior to moving it into a datatable since that is where the issue occurs)

like image 614
confusedandamused Avatar asked Aug 16 '16 17:08

confusedandamused


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.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

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.


1 Answers

The code as you have it will remove the first 5 rows but you also need to do something with the amended file. You could save it in place with:

pack.Save();

or save to a new location with:

pack.SaveAs(new FileInfo(outputFilePath));

I have uploaded a complete example here:

static void Main(string[] args)
{
    var myFileInfo = new FileInfo("Demo.xlsx");
    using (var pack = new ExcelPackage(myFileInfo))
    {
        var ws = pack.Workbook.Worksheets.FirstOrDefault();
        ws.DeleteRow(1, 5, true);
        pack.SaveAs(new FileInfo("output.xlsx"));
    }
}

If you build and run the solution you can see that it transforms the demo file from this in the input file (Demo.xlsx):

input file screengrab

to this in the output file:

output file screengrab

with the first 5 rows removed and everything shifted up.

like image 151
Stewart_R Avatar answered Sep 18 '22 20:09

Stewart_R