簡體   English   中英

Open 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();
            }
        }
    }
}

幫助函數由https://stackoverflow.com/a/182924/5309534提供:

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 存檔打開並打開已從中刪除了單元格的工作表,您將不再看到這些單元格。 但是,如果您在移除的單元格右側有單元格,它們將保留在原處。 要向左移動這些單元格,您需要調整它們的 Cell.CellReference 屬性。 根據工作表的內容,您可能還需要調整其他內容,例如公式、格式、數據驗證、忽略的錯誤等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM