Delete Column from spreadsheet using OpenXML
I was wanting to delete a column using openxml, I am able to clear the contents of the cell, but have been unable to find document开发者_Python百科ation to delete the column as to shift the other cells left when the column is deleted. How may I delete the column using openxml where it will shift the cells to the left?
I discovered that OpenXml has no way to delete a column on its own. Therefor to solve my problem I wrote a function that first removed the contents of the cells in the column I was deleting and then to set the cell reference of the columns that came behind the column I was deleting up a column.
You can iterate for each row and remove the cell you need
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));
}
精彩评论