代码之家  ›  专栏  ›  技术社区  ›  Jason

如何使用带c_的OpenXML SDK v2.0向Excel.xlsx文件添加新工作表?

  •  2
  • Jason  · 技术社区  · 15 年前

    把我今天制定的解决方案贴出来。请看下面我的答案。

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

    如果你知道我用“我不知道…”来评论这些行的目的,请留下一个评论来解释它们。

    3 回复  |  直到 14 年前
        1
  •  4
  •   Jason    15 年前
    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;
    }
    
        2
  •  2
  •   Kim Palmer    14 年前

    (1)我不知道selection.activecell用于什么目的

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

    (2)我不知道板材尺寸。参考用于

    SheetDimension.Reference包含“A4:BA25”等范围 A4是第一个有值的单元格,BA25是最后一个。 我不知道Excel是如何使用这些信息的,但OpenXML不维护空行、空列和空单元格的XML。sheetDimension.reference指示A4之前没有值的单元格,BA25之后没有值的单元格。

        3
  •  0
  •   Kim Palmer    14 年前

    我不知道什么选择。ActiveCell用于

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