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

发布于 2024-08-26 11:49:39 字数 387 浏览 2 评论 0原文

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

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

如果您知道我用“我不知道...”评论的行的目的,请留下评论来解释它们。

Just posting the solution I worked out today. See my answer below.

If you don't have the very helpful OpenXML SDK v2.0 Tool, you can find it at http://www.microsoft.com/downloads/details.aspx?FamilyID=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en

If you know the purpose of the lines I commented with "I don't know...", please leave a comment explaining them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生死何惧 2024-09-02 11:49:39
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;
}
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;
}
决绝 2024-09-02 11:49:39

(1)我不知道Selection.ActiveCell的用途是什么

当Excel打开时,ActiveCell周围有一个焦点矩形。打开新电子表格时,A1 是 ActivCell 默认值。 ActiveCell可以使用Selection.ActiveCell设置为任何单元格

(2)我不知道SheetDimension.Reference用于

SheetDimension.Reference包含一个范围,例如“A4:BA25”
A4 是第一个有值的单元格,BA25 是最后一个。
我不知道 Excel 如何使用此信息,但 OpenXml 不维护空行、列、单元格的 xml。 SheetDimension.Reference 表示没有 A4 之前的单元格,也没有 BA25 之后的单元格

(1) I don't know what Selection.ActiveCell is used for

There is a focus rectangle around the ActiveCell when Excel opens. A1 is the ActivCell default when a new spreadsheet is opened. ActiveCell can be set to any cell using Selection.ActiveCell

(2) I don't know what SheetDimension.Reference is used for

SheetDimension.Reference contanis a Range such as "A4:BA25"
A4 is the first cell with a value and BA25 is the last.
I don't know exactly how Excel uses this info but OpenXml doesn't maintain xml for empty rows, columns, cells. The SheetDimension.Reference indicates that there are no cells with values before A4 and no cells with values after BA25

┈┾☆殇 2024-09-02 11:49:39

我不知道 Selection.ActiveCell 的用途是什么

当 Excel 打开时,ActiveCell 周围有一个焦点矩形。
创建新电子表格时,A1 是 ActivCell 默认值。
ActiveCell 可以使用 Selection.ActiveCell 设置为任何单元格

I don't know what Selection.ActiveCell is used for

There is a focus rectangle around the ActiveCell when Excel opens.
A1 is the ActivCell default when a new spreadsheet is created.
ActiveCell can be set to any cell using Selection.ActiveCell

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文