如何使用 OpenXML 从 Excel 中删除行
How to remove row from Excel using OpenXML
我正在尝试使用以下代码删除行:
SpreadsheetDocument document = SpreadsheetDocument.Open( "test.xlsx", true );
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where( s => s.Name == "sheet1" );
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = ( WorksheetPart )document.WorkbookPart.GetPartById( relationshipId );
IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
Row row = rows.FirstOrDefault<Row>();
row.Remove();
worksheetPart.Worksheet.Save();
document.Close();
但我得到的只是清洁第一个牢房。我需要删除偏移其他行的行。
我该怎么做?
之前
后
我想得到的:
我解决了问题。
注意:
我经验不多,所以下面的代码可能无效
Row row = rows.FirstOrDefault<Row>();
row.Remove();
string cr;
foreach ( Row rowElement in rows )
{
rowElement.RowIndex.Value -= 1;
IEnumerable<Cell> cells = rowElement.Elements<Cell>().ToList();
if ( cells != null )
{
foreach ( Cell cell in cells )
{
cr = cell.CellReference.Value;
int indexRow = Convert.ToInt32( Regex.Replace( cr, @"[^\d]+", "" ) ) - 1;
cr = Regex.Replace( cr, @"[\d-]", "" );
cell.CellReference.Value = $"{cr}{indexRow}";
}
}
}
这就是我设法删除空行的方法
using (SpreadsheetDocument document = SpreadsheetDocument.Open(pathToFile, true))
{
WorkbookPart wbPart = document.WorkbookPart;
var worksheet = wbPart.WorksheetParts.First().Worksheet;
var rows = worksheet.GetFirstChild<SheetData>().Elements<Row>();
// Skip headers
foreach (var row in rows.Skip(1))
{
if (/* some condition on which rows to delete*/)
{
row.Remove();
}
}
// Fix all row indexes
string cr;
for (int i = 2; i < rows.Count(); i++)
{
var newCurrentRowIndex = rows.ElementAt(i - 1).RowIndex.Value + 1;
var currentRow = rows.ElementAt(i);
currentRow.RowIndex.Value = updatedRowIndex;
IEnumerable<Cell> cells = currentRow.Elements<Cell>().ToList();
if (cells != null)
{
foreach (Cell cell in cells)
{
cr = cell.CellReference.Value;
cr = Regex.Replace(cell.CellReference.Value, @"[\d-]", "");
cell.CellReference.Value = $"{cr}{updatedRowIndex}";
}
}
}
worksheet.Save();
}
我正在尝试使用以下代码删除行:
SpreadsheetDocument document = SpreadsheetDocument.Open( "test.xlsx", true );
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where( s => s.Name == "sheet1" );
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = ( WorksheetPart )document.WorkbookPart.GetPartById( relationshipId );
IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
Row row = rows.FirstOrDefault<Row>();
row.Remove();
worksheetPart.Worksheet.Save();
document.Close();
但我得到的只是清洁第一个牢房。我需要删除偏移其他行的行。 我该怎么做?
之前
我想得到的:
我解决了问题。
注意:
我经验不多,所以下面的代码可能无效
Row row = rows.FirstOrDefault<Row>();
row.Remove();
string cr;
foreach ( Row rowElement in rows )
{
rowElement.RowIndex.Value -= 1;
IEnumerable<Cell> cells = rowElement.Elements<Cell>().ToList();
if ( cells != null )
{
foreach ( Cell cell in cells )
{
cr = cell.CellReference.Value;
int indexRow = Convert.ToInt32( Regex.Replace( cr, @"[^\d]+", "" ) ) - 1;
cr = Regex.Replace( cr, @"[\d-]", "" );
cell.CellReference.Value = $"{cr}{indexRow}";
}
}
}
这就是我设法删除空行的方法
using (SpreadsheetDocument document = SpreadsheetDocument.Open(pathToFile, true))
{
WorkbookPart wbPart = document.WorkbookPart;
var worksheet = wbPart.WorksheetParts.First().Worksheet;
var rows = worksheet.GetFirstChild<SheetData>().Elements<Row>();
// Skip headers
foreach (var row in rows.Skip(1))
{
if (/* some condition on which rows to delete*/)
{
row.Remove();
}
}
// Fix all row indexes
string cr;
for (int i = 2; i < rows.Count(); i++)
{
var newCurrentRowIndex = rows.ElementAt(i - 1).RowIndex.Value + 1;
var currentRow = rows.ElementAt(i);
currentRow.RowIndex.Value = updatedRowIndex;
IEnumerable<Cell> cells = currentRow.Elements<Cell>().ToList();
if (cells != null)
{
foreach (Cell cell in cells)
{
cr = cell.CellReference.Value;
cr = Regex.Replace(cell.CellReference.Value, @"[\d-]", "");
cell.CellReference.Value = $"{cr}{updatedRowIndex}";
}
}
}
worksheet.Save();
}