【问题标题】:C# Open XML : We found a problem with some content. Newly added sheet doesn't show any dataC# Open XML:我们发现某些内容存在问题。新添加的工作表不显示任何数据
【发布时间】:2018-12-07 11:09:03
【问题描述】:

以下是我用来添加新工作表到现有电子表格的代码。

我在输入中传递了一个列表,这个 T 只有两个属性“代码”和“描述”。 我循环遍历每个 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();

        }

    }

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

We found some problem with some content

最后表格显示没有任何内容,如下所示:

Blank sheet with sheetname

【问题讨论】:

    标签: c# .net excel openxml worksheet


    【解决方案1】:

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

    试试

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

    【讨论】:

    • 谢谢,我采纳了您的建议并找到了问题---请参阅下面的回答。
    【解决方案2】:

    后来我做了 2 处更改(请参阅下面代码中的 cmets 或将其与原始代码进行比较)——我这样做是因为 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());
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 2020-12-06
      • 2021-04-13
      相关资源
      最近更新 更多