Ajout de plusieurs cellules à une seule ligne

Je suis nouveau dans ce domaine et lorsque j’essaie d’append plusieurs cellules à une ligne, le contenu est illisible. Voici ce que j’ai.

SpreadsheetDocument ssDoc = SpreadsheetDocument.Create(saveFile, SpreadsheetDocumentType.Workbook); // Add a WorkbookPart to the document WorkbookPart workbookPart = ssDoc.AddWorkbookPart(); workbookPart.Workbook = new Workbook(); // Add a WorksheetPart to theWorkbookPart WorksheetPart worksheetPart = workbookPart.AddNewPart(); worksheetPart.Worksheet = new Worksheet(new SheetData()); Sheets sheets = ssDoc.WorkbookPart.Workbook.AppendChild(new Sheets()); Sheet sheet = new Sheet() { Id = ssDoc.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" }; sheets.Append(sheet); Worksheet worksheet = new Worksheet(); SheetData sheetData = new SheetData(); Row row = new Row(); Cell cell = new Cell() { CellReference = "A1", DataType = CellValues.Ssortingng, CellValue = new CellValue("Cell1") }; Cell cell2 = new Cell() { CellReference = "A2", DataType = CellValues.Ssortingng, CellValue = new CellValue("Cell2") }; row.Append(cell); row.Append(cell2); sheetData.Append(row); worksheet.Append(sheetData); worksheetPart.Worksheet = worksheet; // Close the document. ssDoc.Close(); 

Si je supprime la deuxième cellule, cela fonctionne comme prévu.

Au lieu de “A2”, la référence de cellule devrait être “B1”

  SpreadSheet.Cell cell = new SpreadSheet.Cell() { CellReference = "A1", DataType = SpreadSheet.CellValues.Ssortingng, CellValue = new SpreadSheet.CellValue("Cell1") }; SpreadSheet.Cell cell2 = new SpreadSheet.Cell() { CellReference = "B1", DataType = SpreadSheet.CellValues.Ssortingng, CellValue = new SpreadSheet.CellValue("Cell2") }; 

J’ai eu un problème similaire. Si quelqu’un souhaite insérer des cellules dans la même colonne, l’index des lignes doit être incrémenté et la référence de la cellule afin d’insérer une cellule dans la même colonne. Il m’a fallu tout le week-end pour comprendre: D

  Row row2 = new Row { RowIndex = 2}; SpreadSheet.Cell cell2 = new SpreadSheet.Cell() { CellReference = "B1", DataType = SpreadSheet.CellValues.Ssortingng, CellValue = new SpreadSheet.CellValue("Cell2") }; row2.Append(cell2); sheetData.Append(row); 

Il vaut mieux utiliser la boucle.