如何使用OpenXML从Excel中删除列

时间:2014-03-04 07:13:03

标签: c# excel openxml openxml-sdk

我有以下方法使用'Microsoft.Office.Interop.Excel'从Excel中删除列

public static void DeleteExcelColumn(ref Microsoft.Office.Interop.Excel.Worksheet objSheet, List<int> deleteColumns)
    {
        Microsoft.Office.Interop.Excel.Range objCols = objSheet.Columns;
        Microsoft.Office.Interop.Excel.Range objCol = null;
        foreach (int column in deleteColumns)
        {
            objCol = (Microsoft.Office.Interop.Excel.Range)objCols[column,Type.Missing];
            objCol.EntireColumn.Delete(Microsoft.Office.Interop.Excel.XlDeleteShiftDirection.xlShiftToLeft);
        }

        System.Runtime.InteropServices.Marshal.ReleaseComObject(objCols);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(objCol);
    }

如何使用“OpenXML”

实现相同目标
public static void DeleteExcelColumnUsingOpenXML(ref DocumentFormat.OpenXml.Spreadsheet.Worksheet objSheet, List<int> deleteColumns)
{
         SheetData sheetData = objSheet.GetFirstChild<SheetData>();
        foreach (int column in deleteColumns)
        {
            sheetData.Elements<DocumentFormat.OpenXml.Spreadsheet.Columns>() ???
            // delete column
        }
}