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

时间:2018-12-07 11:09:03

标签: c# .net excel openxml worksheet

下面是iam用于将新工作表添加到现有电子表格中的代码。

我在输入中传递了一个列表,此T仅具有2个属性“ code”和“ description”。 我正在遍历每个T属性,并将它们放入工作表数据中,最后保存了Speshseet。

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();

        }

    }

问题是一切都发生了而没有错误,我的意思是我可以在调试窗口中看到添加的工作表,我也保存了所有内容,但是当我打开该电子表格时,我看到了一条错误消息

We found some problem with some content

最后,工作表显示如下,没有任何内容:

Blank sheet with sheetname

2 个答案:

答案 0 :(得分:1)

我认为问题在于您在声明sheetdata的地方,因为SheetData应该已经存在。

尝试

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

答案 1 :(得分:0)

后来我做了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());


    }