I have to remove empty Cells from Excel Sheets. We using to create and edit files ClosedXML.
Example Excel-Before:
XX XX XX XX XX
XX XX XX XX XX
XX XX XX XX XX XX
XX XX XX XX XX XX
Example Excel-After:
XX XX XX XX XX
XX XX XX XX XX
XX XX XX XX XX XX
XX XX XX XX XX XX
My first test was a foreach over all empty Cells. But this is not working, because only the first cell in Row will be deleted.
private void DeleteCell()
{
List<IXLCell> AllEmptyCells = ws.Cells().Where(w => String.IsNullOrEmpty(w.Value.ToString())).ToList();
foreach(IXLCell cell in AllEmptyCells)
{
cell.Delete(XLShiftDeletedCells.ShiftCellsLeft);
}
}
My second test is working, but needs a long time to finish. With every call the list will be new generated until the list is empty.
private void DeleteCell()
{
List<IXLCell> AllEmptyCells = ws.Cells().Where(w => String.IsNullOrEmpty(w.Value.ToString())).ToList();
if(AllEmptyCells.Count > 0)
{
ws.Cell(AllEmptyCells.FirstOrDefault().Address).Delete(XLShiftDeletedCells.ShiftCellsLeft);
DeleteCell();
}
}
For 100 Rows this part will running ~2 Min. Does anyone knows a better way?