I have a sample.xlsx file generated by a program(of which I have no control) with data spanning across 200 rows. However, the file has another 1800 "trailing" empty rows(as shown in image) saved into it making it a total of 2000 rows. ie, if I read this file using openXML and get the rowcount, it is 2000 instead of 200.
Am trying to figure out a way to delete these rows so that the excel will be having only rows with data.
NOTE : There are no empty rows in between.
All the empty rows comes at the end of the excel only. How can I check for empty rows and delete it or Find out the rows with value and delete the remainig rows by row index ?
PS : I can't use Interop Services. And also the counts 200 and 2000 are for example purpose, it may vary from one file to another.
Below is my code:
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(@"C:\sample.xlsx", true))
{
WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
var thisSheet = workbookPart.Workbook.Descendants<Sheet>()
.FirstOrDefault(s => s.Name == "sheet1");
SheetData sheetData = thisSheet.GetFirstChild<SheetData>();
IEnumerable<Row> rows = sheetData.Descendants<Row>();
foreach (var row in rows)
{
//method to find empty rows or row index of empty rows
// rows.Remove() or RemoveAllChildren()
}
}