簡體   English   中英

如何使用帶有c#的OpenXML SDK v2.0將新工作表添加到Excel .xlsx文件中?

[英]How do I add a new sheet to an Excel .xlsx file using the OpenXML SDK v2.0 with c#?

剛剛發布了我今天制定的解決方案。 請參閱下面的答案。

如果您沒有非常有用的OpenXML SDK v2.0工具,則可以在以下網址找到它: http://www.microsoft.com/downloads/details.aspx? FamilyID=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en

如果您知道我用“我不知道...”注釋的行的目的,請留下評論以解釋它們。

using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(spreadSheetFileName, true)) {
    WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;

    // rId must be unique within the spreadsheet. 
    // You might be able to use the SpreadSheetDocument.Parts.Count() to do this.
    // i.e. string relationshipID = "rId" + (spreadsheetDocument.Parts.Count() + 1).ToString();
    string rId = "rId6";

    // Sheet.Name and Sheet.SheetId must be unique within the spreadsheet.
    Sheet sheet = new Sheet() { Name = "Sheet4", SheetId = 4U, Id = rId };
    workbookPart.Workbook.Sheets.Append(sheet);

    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(rId);

    Worksheet worksheet = new Worksheet();
    worksheet.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");

    // I don't know what SheetDimension.Reference is used for, it doesn't seem to change the resulting xml.
    SheetDimension sheetDimension = new SheetDimension() { Reference = "A1:A3" };
    SheetViews sheetViews = new SheetViews();
    // If more than one SheetView.TabSelected is set to true, it looks like Excel just picks the first one.
    SheetView sheetView = new SheetView() { TabSelected = false, WorkbookViewId = 0U };

    // I don't know what Selection.ActiveCell is used for, it doesn't seem to change the resulting xml.
    Selection selection = new Selection() { ActiveCell = "A1", SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A1" } };
    sheetView.Append(selection);
    sheetViews.Append(sheetView);
    SheetFormatProperties sheetFormatProperties = new SheetFormatProperties() { DefaultRowHeight = 15D };

    SheetData sheetData = new SheetData();

    // I don't know what the InnerText of Row.Spans is used for. It doesn't seem to change the resulting xml.
    Row row = new Row() { RowIndex = 1U, Spans = new ListValue<StringValue>() { InnerText = "1:3" } };

    Cell cell1 = new Cell() { CellReference = "A1", DataType = CellValues.Number, CellValue = new CellValue("99") };
    Cell cell2 = new Cell() { CellReference = "B1", DataType = CellValues.Number, CellValue = new CellValue("55") };
    Cell cell3 = new Cell() { CellReference = "C1", DataType = CellValues.Number, CellValue = new CellValue("33") };

    row.Append(cell1);
    row.Append(cell2);
    row.Append(cell3);

    sheetData.Append(row);
    PageMargins pageMargins = new PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.7D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D };

    worksheet.Append(sheetDimension);
    worksheet.Append(sheetViews);
    worksheet.Append(sheetFormatProperties);
    worksheet.Append(sheetData);
    worksheet.Append(pageMargins);

    worksheetPart.Worksheet = worksheet;
}

(1)我不知道Selection.ActiveCell用於什么

Excel打開時,ActiveCell周圍有一個焦點矩形。 打開新電子表格時,A1是ActivCell的默認值。 可以使用Selection.ActiveCell將ActiveCell設置為任何單元格

(2)我不知道將SheetDimension.Reference用於什么

SheetDimension.Reference包含一個范圍,例如“ A4:BA25”。A4是第一個具有值的單元格,BA25是最后一個單元格。 我不完全知道Excel如何使用此信息,但是OpenXml不會為空行,列和單元格維護xml。 SheetDimension.Reference指示沒有單元格的值在A4之前,沒有單元格的值在BA25之后

我不知道什么Selection.ActiveCell用於

Excel打開時,ActiveCell周圍有一個焦點矩形。 創建新電子表格時,A1是ActivCell默認值。 可以使用Selection.ActiveCell將ActiveCell設置為任何單元格

暫無
暫無

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

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