9 Stimmen

Excel fügt Daten zu WorksheetPart im Code dahinter hinzu

Hallo Leute, ich erstelle eine Excel-Datei mit 3 Arbeitsblättern mit folgendem Code.

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Create(path + @"\UrlReport.xlsx", SpreadsheetDocumentType.Workbook))
            {
                // create the workbook
                spreadSheet.AddWorkbookPart();
                spreadSheet.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();

                // CREATE FIRST SHEET
                WorksheetPart newWorksheetPart1 = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();

                newWorksheetPart1.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

                // create sheet data
                newWorksheetPart1.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                // save worksheet
                newWorksheetPart1.Worksheet.Save();

                // create the worksheet to workbook relation
                spreadSheet.WorkbookPart.Workbook.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheets());
                spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                {
                    Id = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart1),
                    SheetId = 1,
                    Name = "Sheet1"
                });

                //CREATE SECOND SHEET
                WorksheetPart newWorksheetPart2 = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart2.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

                // create sheet data
                newWorksheetPart2.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                // save worksheet
                newWorksheetPart2.Worksheet.Save();

                // create the worksheet to workbook relation
                spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                {
                    Id = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart2),
                    SheetId = 2,
                    Name = "Sheet2"

                });

                //CREATE THIRD SHEET
                WorksheetPart newWorksheetPart3 = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart3.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet();

                // create sheet data
                newWorksheetPart3.Worksheet.AppendChild(new DocumentFormat.OpenXml.Spreadsheet.SheetData());

                // save worksheet
                newWorksheetPart3.Worksheet.Save();

                // create the worksheet to workbook relation
                spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>().AppendChild(new DocumentFormat.OpenXml.Spreadsheet.Sheet()
                {
                    Id = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart3),
                    SheetId = 3,
                    Name = "Sheet3"
                });

                spreadSheet.WorkbookPart.Workbook.Save();
            }

Ich muss wissen, wie ich die Zellen in den 3 Datenblättern ausfüllen kann. Ich muss nur Zeichenfolgen zu einigen Zellen hinzufügen, die wahrscheinlich in einer Datentabelle oder einem mehrdimensionalen Array sein werden.

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X