C# Open XML:我们发现某些内容存在问题。新添加的 sheet 没有显示任何数据

C# Open XML : We found a problem with some content. Newly added sheet doesn't show any data

下面是我用来 ADD 一个新的 sheet 到已经存在的价差 sheet.

的代码

我在输入中传递一个 list,这个 T 只有 2 个属性 "code" 和 "description"。 我正在遍历每个 T 属性并将它们放入 sheetdata 中,最后保存 spreadhseet。

private static void PutInExcel(List<RulesEngineOutput> output)
    {
        using (SpreadsheetDocument document = SpreadsheetDocument.Open(@"C:\ATP\Sprints\PA\RE\IO.xlsx", true))
        {


            // Add a blank WorksheetPart.
            WorksheetPart newWorksheetPart = document.WorkbookPart.AddNewPart<WorksheetPart>();
            newWorksheetPart.Worksheet = new Worksheet(new SheetData());

            Sheets sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = document.WorkbookPart.GetIdOfPart(newWorksheetPart);

            // Get a unique ID for the new worksheet.
            uint sheetId = 1;
            if (sheets.Elements<Sheet>().Count() > 0)
            {
                sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            // Give the new worksheet a name.
            string sheetName = "NewRole" + sheetId;

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
            sheets.Append(sheet);

            SheetData sheetData = newWorksheetPart.Worksheet.AppendChild(new SheetData());
            // Constructing header
            Row row = new Row();

            row.Append(
                ConstructCell("Code", CellValues.String),
                ConstructCell("Description", CellValues.String));

            // Insert the header row to the Sheet Data
            sheetData.AppendChild(row);


            foreach (var reItem in output)
            {
                row = new Row();

                row.Append(
                    ConstructCell(reItem.Code.ToString(), CellValues.Number),
                    ConstructCell(reItem.Description, CellValues.String)
                    );

                sheetData.AppendChild(row);
            }


            newWorksheetPart.Worksheet.Save();
            document.WorkbookPart.Workbook.Save();
            document.Save();

        }

    }

问题是一切都没有错误,我的意思是我可以在调试 window 中看到添加的工作 sheet,我也保存了所有内容,但是当我打开那个 spreadhseet 时,我看到一条错误消息

We found some problem with some content

最后显示的 sheet 没有任何内容,如下所示:

Blank sheet with sheetname

我认为问题出在您在哪里声明 sheetdata,因为 SheetData 应该已经存在。

尝试

SheetData sheetData = newWorksheetPart.Worksheet.GetFirstChild<SheetData>();

后来我做了 2 处更改(请参阅下面代码中的注释或将其与原始代码进行比较)——我这样做是因为 xml 出错了所以我发现将代码转换为数字是xml 错误的情况。 此外,Alan H 建议尝试消除工作表内部传递的 new SheetData(),因此我使用了默认的空构造函数。

private static void PutInExcel(List<RulesEngineOutput> output)
    {
        using (SpreadsheetDocument document = SpreadsheetDocument.Open(@"C:\ATP\Sprints\PA\RE\IO.xlsx", true))
        {


            // Add a blank WorksheetPart.
            WorksheetPart newWorksheetPart = document.WorkbookPart.AddNewPart<WorksheetPart>();
            newWorksheetPart.Worksheet = new Worksheet(); // Change 1

            Sheets sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = document.WorkbookPart.GetIdOfPart(newWorksheetPart);

            // Get a unique ID for the new worksheet.
            uint sheetId = 1;
            if (sheets.Elements<Sheet>().Count() > 0)
            {
                sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
            }

            // Give the new worksheet a name.
            string sheetName = "NewRole" + sheetId;

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
            sheets.Append(sheet);

            SheetData sheetData = newWorksheetPart.Worksheet.AppendChild(new SheetData());
            // Constructing header
            Row row = new Row();

            row.Append(
                ConstructCell("Code", CellValues.String), // Change 2
                ConstructCell("Description", CellValues.String));

            // Insert the header row to the Sheet Data
            sheetData.AppendChild(row);


            foreach (var reItem in output)
            {
                row = new Row();

                row.Append(
                    ConstructCell(reItem.Code.ToString(), **CellValues.String**),
                    ConstructCell(reItem.Description, CellValues.String)
                    );

                sheetData.AppendChild(row);
            }


            newWorksheetPart.Worksheet.Save();
            document.WorkbookPart.Workbook.Save();
            document.Save();


        }

        //string csv = String.Join(",", output.Select(x => x.ToString()).ToArray());


    }