Come faccio ad aggiungere un nuovo foglio in un file xlsx di Excel utilizzando il v2.0 OpenXML SDK con C #?

StackOverflow https://stackoverflow.com/questions/2495390

  •  21-09-2019
  •  | 
  •  

Domanda

Basta inviare la soluzione ho lavorato fuori oggi. Vedere la mia risposta qui sotto.

Se non si dispone di molto utile OpenXML SDK v2.0 strumento, si può trovare all'indirizzo http://www.microsoft.com/downloads/details.aspx?FamilyID=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en

Se si conosce lo scopo delle linee che ha commentato con "Non lo so ...", si prega di lasciare un commento spiegando loro.

È stato utile?

Soluzione

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;
}

Altri suggerimenti

(1) Io non so cosa Selection.ActiveCell viene utilizzato per

C'è un rettangolo di attivazione attorno al ActiveCell quando si apre Excel. A1 è l'impostazione predefinita ActivCell quando un nuovo foglio di calcolo viene aperto. ActiveCell può essere impostato su qualsiasi cellulare utilizzando Selection.ActiveCell

(2) Io non so cosa SheetDimension.Reference viene utilizzato per

SheetDimension.Reference contanis un intervallo, ad esempio "A4: BA25" A4 è la prima cella con un valore e BA25 è l'ultimo. Non so esattamente come Excel utilizza queste informazioni, ma OpenXml non mantiene XML per righe vuote, colonne, celle. Lo SheetDimension.Reference indica che non ci sono le celle con i valori prima A4 e nessun celle con valori dopo BA25

Non so che cosa Selection.ActiveCell viene utilizzato per

C'è un rettangolo di attivazione attorno al ActiveCell quando si apre Excel. A1 è l'impostazione predefinita ActivCell quando viene creato un nuovo foglio di calcolo. ActiveCell può essere impostato su qualsiasi cellulare utilizzando Selection.ActiveCell

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top