打开 XML:使用列索引删除整个 excel 列
Open XML: Delete entire excel column using column index
我有电子表格中 excel 列的列索引,需要使用此列索引删除整个列。我只能使用 Open XML SDK 2.0.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace openXMLDemo
{
public class Program
{
public static void Main(string[] args)
{
string fileFullPath = @"path to the excel file here";
string sheetName = "excelsheet name here";
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileFullPath, true))
{
Sheet sheet = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
if (sheet != null)
{
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id.Value);
// This is where I am struggling....
// finding the reference to entire column with the use of column index
Column columnToDelete = sheet.GetFirstChild<SheetData>().Elements<Column>()
}
}
}
}
}
Open XML 不幸的是无法选择列,因此您需要遍历每一行和其中的单元格以删除数据:
static void Main(string[] args)
{
string fileFullPath = @"C:\Book1.xlsx";
string sheetName = "Sheet1";
// Specify your column index and then convert to letter format
int columnIndex = 3;
string columnName = GetExcelColumnName(columnIndex);
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileFullPath, true)) {
Sheet sheet = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
if (sheet != null) {
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id.Value);
// Get all the rows in the workbook
IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
// Ensure that there are actually rows in the workbook
if (rows.Count() == 0){
return;
}
// Select all the cells from each row where the column letter is equal to index
foreach (Row row in rows) {
var cellsToRemove = row.Elements<Cell>().Where(x => new String(x.CellReference.Value.Where(Char.IsLetter).ToArray()) == columnName);
foreach (var cell in cellsToRemove)
cell.Remove();
}
}
}
}
辅助函数由 提供:
static string GetExcelColumnName(int columnNumber)
{
int dividend = columnNumber;
string columnName = String.Empty;
int modulo;
while (dividend > 0) {
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (int)((dividend - modulo) / 26);
}
return columnName;
}
@Shimmy Weitzhandler。 OpenXML 不模拟 Excel。 @Chawin 提出的代码确实会删除单元格。如果您将工作簿作为 zip 存档打开并打开其中已删除单元格的 sheet,您将不再看到这些单元格。但是,如果您在移除的单元格右侧有单元格,它们将保留在原处。要向左移动这些单元格,您需要调整它们的 Cell.CellReference 属性。根据 sheet 的内容,您可能还需要调整其他内容,例如公式、格式、数据验证、忽略的错误等。
我有电子表格中 excel 列的列索引,需要使用此列索引删除整个列。我只能使用 Open XML SDK 2.0.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace openXMLDemo
{
public class Program
{
public static void Main(string[] args)
{
string fileFullPath = @"path to the excel file here";
string sheetName = "excelsheet name here";
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileFullPath, true))
{
Sheet sheet = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
if (sheet != null)
{
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id.Value);
// This is where I am struggling....
// finding the reference to entire column with the use of column index
Column columnToDelete = sheet.GetFirstChild<SheetData>().Elements<Column>()
}
}
}
}
}
Open XML 不幸的是无法选择列,因此您需要遍历每一行和其中的单元格以删除数据:
static void Main(string[] args)
{
string fileFullPath = @"C:\Book1.xlsx";
string sheetName = "Sheet1";
// Specify your column index and then convert to letter format
int columnIndex = 3;
string columnName = GetExcelColumnName(columnIndex);
using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileFullPath, true)) {
Sheet sheet = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
if (sheet != null) {
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id.Value);
// Get all the rows in the workbook
IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();
// Ensure that there are actually rows in the workbook
if (rows.Count() == 0){
return;
}
// Select all the cells from each row where the column letter is equal to index
foreach (Row row in rows) {
var cellsToRemove = row.Elements<Cell>().Where(x => new String(x.CellReference.Value.Where(Char.IsLetter).ToArray()) == columnName);
foreach (var cell in cellsToRemove)
cell.Remove();
}
}
}
}
辅助函数由 提供:
static string GetExcelColumnName(int columnNumber)
{
int dividend = columnNumber;
string columnName = String.Empty;
int modulo;
while (dividend > 0) {
modulo = (dividend - 1) % 26;
columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
dividend = (int)((dividend - modulo) / 26);
}
return columnName;
}
@Shimmy Weitzhandler。 OpenXML 不模拟 Excel。 @Chawin 提出的代码确实会删除单元格。如果您将工作簿作为 zip 存档打开并打开其中已删除单元格的 sheet,您将不再看到这些单元格。但是,如果您在移除的单元格右侧有单元格,它们将保留在原处。要向左移动这些单元格,您需要调整它们的 Cell.CellReference 属性。根据 sheet 的内容,您可能还需要调整其他内容,例如公式、格式、数据验证、忽略的错误等。