Excel 创建带有填充数据的命名工作表

Excel create named sheets with populated data

近 3 周以来,我一直在努力让它发挥作用。该代码假设创建一个 excel 文档,每个项目都有一个新命名的 sheet,将对象数据传输到 sheet。不幸的是,它将数据复制到所有 sheet 中。

SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create
                        (filepath, SpreadsheetDocumentType.Workbook);

                // Add a WorkbookPart to the document.
                WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
                workbookpart.Workbook = new Workbook();

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

                // Add Sheets to the Workbook
                Sheets sheets = workbookpart.Workbook.
                    AppendChild(new Sheets());



                // Append a new worksheet and associate it with the workbook.
                SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

                foreach (Town temp in CampaignTowns)
                {

                    // Constructing header
                    Row row = new Row();


                    row = CreateContentRow(temp, sheetData);
                    // Insert the header row to the Sheet Data

                    //sheetData.AppendChild(row);
                    Sheet sheet = new Sheet()
                    {
                        Id = workbookpart.GetIdOfPart(worksheetPart),
                        SheetId = TempSheetId,
                        Name = temp.getName()
                    };
                    TempSheetId++;
                    sheets.Append(sheet);


                    sheetData = new SheetData();

                }
                //Save worksheet part
                worksheetPart.Worksheet.Save();
                workbookpart.Workbook.Save();
                // Close the document.
                spreadsheetDocument.Close();

创建一个包含您想要的三个 sheet 的工作簿(在 Excel 中)。在每个 sheet 上放一点数据。保存并关闭工作簿。

打开 Open XML SDK Productivity Tool(可从 Microsoft 网站下载)。在工具中打开您的新工作簿。使用 Reflect Code 按钮打开代表您的文档的 OpenXML 代码。

Copy/paste 您需要的代码。让这样的东西工作是最容易的。祝你好运。

所以在做了一些研究之后,我发现了我的错误并更正了它。显然,您必须在循环中初始化 Worksheet 部分和 SheetData。这是我的工作更正代码的副本,适用于任何有相同错误的人。我还将我的 CreateContentRow 函数更正为无效。

SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create
                        (filepath, SpreadsheetDocumentType.Workbook);

                // Add a WorkbookPart to the document.
                WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
                workbookpart.Workbook = new Workbook();

                // Add Sheets to the Workbook
                Sheets sheets = workbookpart.Workbook.
                    AppendChild(new Sheets());

                foreach (Town temp in CampaignTowns)
                {
                    // Add a WorksheetPart to the WorkbookPart.
                    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                    worksheetPart.Worksheet = new Worksheet();

                    worksheetPart.Worksheet.Save();

                    // Append a new worksheet and associate it with the workbook.
                    SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());

                    // Insert the header row to the Sheet Data
                    CreateContentRow(temp, sheetData);

                    Sheet sheet = new Sheet()
                    {
                        Id = workbookpart.GetIdOfPart(worksheetPart),
                        SheetId = TempSheetId,
                        Name = temp.getName()
                    };
                    TempSheetId++;
                    sheets.AppendChild(sheet);

                }
                // Close the document.
                spreadsheetDocument.Close();