i'am trying to export data to Excel. For that purpose i use OpenXML.
The Structure of the Excel should look like this:
My current Code:
- public void CreateExcelDoc(string fileName)
- {
- using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
- {
- WorkbookPart workbookPart = document.AddWorkbookPart();
- workbookPart.Workbook = new Workbook();
-
- WorksheetPart worksheetPart = workbookPart.AddNewPart();
- worksheetPart.Worksheet = new Worksheet();
-
- Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
-
- Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Test Sheet" };
-
- sheets.Append(sheet);
-
- workbookPart.Workbook.Save();
-
-
- SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
-
-
-
-
- Row row = new Row();
-
- row.Append(
- ConstructCell("Vendor Name", CellValues.String),
- ConstructCell("Vendor Mail", CellValues.String),
- ConstructCell("Vendor Adress", CellValues.String),
- ConstructCell("Vendor Phone", CellValues.String));
-
-
- sheetData.AppendChild(row);
-
- ServicesExportDataProvider dp = new ServicesExportDataProvider();
- var vendors = dp.GetVendors();
-
-
-
-
- foreach (var vendor in vendors)
- {
- row = new Row();
-
- row.Append(
- ConstructCell(vendor.VendorName, CellValues.String),
- ConstructCell(vendor.VendorMail, CellValues.String),
- ConstructCell(vendor.VendorAdress, CellValues.String),
- ConstructCell(vendor.VendorPhone, CellValues.String));
-
- sheetData.AppendChild(row);
- }
-
- InsertEmptyRow();
-
-
-
- Column column = new Column();
- Cell cell = ConstructCell("Info", CellValues.String);
- column.Append(cell);
-
-
- sheetData.AppendChild(column);
-
-
-
-
- worksheetPart.Worksheet.Save();
- }
- }
I can export the "Vendors" - Lines. But if i try then to Create columns to Export the "Item Information" vertically, i get the following error:
Non-composite elements do not have child elements
- Column column = new Column();
- Cell cell = ConstructCell("Info", CellValues.String);
- column.Append(cell);
I can't figure out the problem. Do you have any ideas? How can I Export data in the structure of "Item Information"?