C# 打开XML:使用列索引删除整个excel列

C# 打开XML:使用列索引删除整个excel列,c#,excel,openxml-sdk,C#,Excel,Openxml Sdk,我已经在电子表格中获得了excel列的列索引,需要使用此列索引删除整个列。我仅限于使用OpenXMLSDK2.0 using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; namespace openXMLDemo { public class Program { public static void Main(string[] args)

我已经在电子表格中获得了excel列的列索引,需要使用此列索引删除整个列。我仅限于使用OpenXMLSDK2.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>()
                }
            }
        }
    }    
}
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Spreadsheet;
名称空间openXMLDemo
{
公共课程
{
公共静态void Main(字符串[]args)
{
字符串fileFullPath=@“此处excel文件的路径”;
string sheetName=“excelsheet name here”;
使用(SpreadsheetDocument=SpreadsheetDocument.Open(fileFullPath,true))
{
Sheet Sheet=document.WorkbookPart.Workbook.GetFirstChild().Elements()。其中(s=>s.Name==sheetName.FirstOrDefault();
如果(工作表!=null)
{
工作表部件工作表部件=(工作表部件)document.WorkbookPart.GetPartById(sheet.Id.Value);
//这就是我挣扎的地方。。。。
//使用列索引查找对整个列的引用
Column columntodelet=sheet.GetFirstChild().Elements()
}
}
}
}    
}

不幸的是,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();
            }
        }
    }
}

@希米·魏茨汉德勒。OpenXML不模拟Excel。@Chawin提出的代码确实会删除这些单元格。如果将工作簿作为zip存档打开,并打开从中删除单元格的工作表,则将不再看到这些单元格。但是,如果从移除的单元格中有右侧的单元格,它们将保持原来的状态。要向左移动这些单元格,需要调整其Cell.CellReference属性。根据工作表的内容,您可能还需要调整其他内容,如公式、格式、数据验证、忽略的错误等。

是否要删除列数据,或删除列布局/样式信息?(
元素仅包含电子表格中[一系列]列的布局/样式信息,实际数据存储在
单元格
元素中,包含在
SheetData
元素中的单个
元素中)它不删除列,只清除单元格并将其留空。
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;
}