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

标签 c# excel-2007 openxml-sdk

只是发布我今天制定的解决方案。请参阅下面的答案。

如果您没有非常有用的 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;
}

关于c# - 如何使用带有 c# 的 OpenXML SDK v2.0 将新工作表添加到 Excel .xlsx 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2495390/

相关文章:

Excel - 拆分列表

c# - OpenXML 从工作表中获取工作表名称

c# - 无法克隆带有图像的新 PowerPoint 幻灯片

c# - 匹配次数最多的子序列?

c# - 将 SQL 逻辑转换为高性能 LINQ

excel-2007 - oracle bipublisherexcel模板报告错误-输出失败

c# - 如何使用 Open XML SDK(XLSX) 在 Excel 中创建数据透视表

c# - C#中的 float 大括号

c# - 当 StackPanel 宽度为全宽时,WP8 ListBox DataTemplate 抛出异常

vba - 计算VBA中不同单元格的数量