0
我需要從Excel文件中刪除某些行。 excel文件是一個模板,可能會有額外的行,必須刪除最終的輸出。但是,刪除單元會破壞文件。代碼如下簡單使用OpenXML從Excel中刪除行
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
foreach (var row in sheetData.Elements<Row>().
Where(r => r.RowIndex.Value >= rowIndex && r.RowIndex.Value < rowIndex + count).ToList())
{
row.Remove();
}
另外,我試着設置現有的單元格爲空值。
Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
foreach (var row in sheetData.Elements<Row>().
Where(r => r.RowIndex.Value >= rowIndex && r.RowIndex.Value < rowIndex + count).ToList())
{
IEnumerable<Cell> cells = row.Elements<Cell>().ToList();
if (cells != null)
{
foreach (Cell cell in cells)
{
if (cell.CellValue != null && !String.IsNullOrEmpty(cell.CellValue.Text))
{
cell.DataType = new EnumValue<CellValues>(CellValues.String);
cell.CellValue = new CellValue("");
}
}
}
}
我的文件總是被破壞。