Delete a column from a table using OpenXML

I wanted to delete a column using openxml, I can clear the contents of the cell, but I could not find the documentation for deleting the column to move the rest of the cells left after deleting the column. How to delete a column using openxml, where will it shift the cells to the left?

+3
source share
3 answers

I found that OpenXml does not have the ability to delete the column myself. Therefore, to solve my problem, I wrote a function that first deleted the contents of the cells in the column that I deleted, and then, to establish a link to the cell of the columns that came after the column, I deleted the column.

+4
source

You can iterate over each row and delete the desired cell

private void RemoveCell(int rowIndex, int colIndex)
{
    SheetData sheetData = _worksheetPart.Worksheet.GetFirstChild<SheetData>(); // get the sheet
    Row row = sheetData.Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
    if (row != null && row.Count() > colIndex)
        row.RemoveChild(row.ElementAt(colIndex));
}
+1

I found that OpenXml cannot delete a column on its own. Therefore, to solve my problem, I wrote a function that first deleted the contents of the cells in the column that I deleted, and then set a link to the cell of the columns that were behind the column that I deleted, up the column.

Hi Amanda, please share a solution for this.

0
source

Source: https://habr.com/ru/post/1793916/


All Articles