2016-05-12 42 views

risposta

13

È possibile utilizzare le classi MergeCells e MergeCell per creare le celle unite richiesti. La classe MergeCells è la raccolta di celle di unione (<mergeCells count="3"> nel codice XML) e la classe MergeCell rappresenta ogni singolo insieme di celle unite (<mergeCell ref="xx:xx" /> nel codice XML). Per popolare i dati nelle celle unite è necessario aggiungere il valore alla cella più in alto a sinistra; qualsiasi altro valore sarà ignorato.

Il codice seguente creerà un nuovo file con celle unite.

using (SpreadsheetDocument myDoc = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook)) 
{ 
    WorkbookPart workbookpart = myDoc.AddWorkbookPart(); 
    workbookpart.Workbook = new Workbook(); 

    // Add a WorksheetPart to the WorkbookPart. 
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>(); 

    SheetData sheetData = new SheetData(); 

    //add a row 
    Row firstRow = new Row(); 
    firstRow.RowIndex = (UInt32)1; 

    //create a cell in C1 (the upper left most cell of the merged cells) 
    Cell dataCell = new Cell(); 
    dataCell.CellReference = "C1"; 
    CellValue cellValue = new CellValue(); 
    cellValue.Text = "99999"; 
    dataCell.Append(cellValue); 

    firstRow.AppendChild(dataCell); 

    sheetData.AppendChild(firstRow); 
    // Add a WorkbookPart to the document. 
    worksheetPart.Worksheet = new Worksheet(sheetData); 

    //create a MergeCells class to hold each MergeCell 
    MergeCells mergeCells = new MergeCells(); 

    //append a MergeCell to the mergeCells for each set of merged cells 
    mergeCells.Append(new MergeCell() { Reference = new StringValue("C1:F1") }); 
    mergeCells.Append(new MergeCell() { Reference = new StringValue("A3:B3") }); 
    mergeCells.Append(new MergeCell() { Reference = new StringValue("G5:K5") }); 

    worksheetPart.Worksheet.InsertAfter(mergeCells, worksheetPart.Worksheet.Elements<SheetData>().First()); 

    //this is the part that was missing from your code 
    Sheets sheets = myDoc.WorkbookPart.Workbook.AppendChild(new Sheets()); 
    sheets.AppendChild(new Sheet() 
    { 
     Id = myDoc.WorkbookPart.GetIdOfPart(myDoc.WorkbookPart.WorksheetParts.First()), 
     SheetId = 1, 
     Name = "Sheet1" 
    }); 
} 

Il codice precedente produce:

enter image description here