【问题标题】:OpenXML created SpreadSheet found unreadable contentOpenXML 创建的电子表格发现不可读的内容
【发布时间】:2019-03-18 02:51:39
【问题描述】:

我正在使用 OpenXML 文件创建一个 Excel 文件,如下所示

 class Program
{
    static void Main(string[] args)
    {
        Dictionary<string, string> dic1 = new Dictionary<string, string>
        {
            {"H1","FPT1" },
            {"AA1","IPN1" },
        };

        Dictionary<string, string> dic2 = new Dictionary<string, string>
        {
            {"H2","FPT2" },
            {"AA2","IPN2" },
        };
        Dictionary<string, string> dic3 = new Dictionary<string, string>
        {
            {"H3","FPT3" },
            {"AA3","IPN3" },
        };
        Dictionary<string, string> dic4 = new Dictionary<string, string>
        {
            {"H4","FPT4" },
            {"AA4","IPN4" },
        };
        List<Dictionary<string, string>> data = new List<Dictionary<string, string>>();
        data.Add(dic1);
        data.Add(dic2);
        data.Add(dic3);
        data.Add(dic4);

        CreateSpreadsheetWorkbook(@"J:\OpenXMLApp\Myfile.xlsx", data);
    }

    public static void CreateSpreadsheetWorkbook(string filepath, List<Dictionary<string, string>> Data)
    {
        // Create a spreadsheet document by supplying the filepath.
        // By default, AutoSave = true, Editable = true, and Type = xlsx.
        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(new SheetData());

        // Add Sheets to the Workbook.
        Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

        // Append a new worksheet and associate it with the workbook.
        Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
        sheets.Append(sheet);

        // Get the sheetData cell table.
        SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

        int rowindex = 2;
        foreach (Dictionary<string, string> _row in Data)
        {
            Row row = new Row
            {
                RowIndex = (uint)rowindex
            };
            sheetData.Append(row);
            foreach (KeyValuePair<string, string> pair in _row)
            {
                string CellAddressForCurrentAttribute = string.Empty;

                Cell refcell = row.Elements<Cell>().Where(c => string.Compare(c.CellReference.Value, CellAddressForCurrentAttribute, true) == 0).FirstOrDefault();
                Cell newCell = new Cell { CellReference = pair.Key };
                newCell.InlineString = new InlineString() { Text = new Text(pair.Value) };
                newCell.DataType = CellValues.SharedString;
                row.InsertBefore(newCell, refcell);
            }
        }
        rowindex++;
        spreadsheetDocument.Save();
        // Close the document.
        spreadsheetDocument.Close();
    }
}

现在,当我打开由上述代码创建的 Excel 文件时,出现以下错误。

在日志文件中我得到如下

    <?xml version="1.0" encoding="UTF-8"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
   <logFileName>error199640_01.xml</logFileName>
   <summary>Errors were detected in file 'J:\OpenXMLApp\Myfile.xlsx'</summary>
   <removedRecords>
      <removedRecord>Removed Records: Cell information from /xl/worksheets/sheet1.xml part</removedRecord>
   </removedRecords>
   <repairedRecords>
      <repairedRecord>Repaired Records: Cell information from /xl/worksheets/sheet1.xml part</repairedRecord>
   </repairedRecords>
</recoveryLog>

OpenXML 在生成 Excel 文件时有什么问题?我正在使用来自 MSDN 网站 here 的示例 sn-p .................... ......

【问题讨论】:

    标签: c# openxml


    【解决方案1】:

    OpenXML 就像一个固执的女朋友。它需要以特定的方式完成事情,如果你改变哪怕是最轻微的事情,它就会崩溃。

    请在下面找到修改后的代码,希望它可以按照您想要的方式工作。我已经对其进行了测试,它会生成输出而不会出现任何错误。

    class Program
        {
            static void Main(string[] args)
            {
                var dic1 = new Dictionary<string, string>
            {
                {"H2","FPT1" },
                {"AA2","IPN1" },
            };
    
                var dic2 = new Dictionary<string, string>
            {
                {"H3","FPT2" },
                {"AA3","IPN2" },
            };
                var dic3 = new Dictionary<string, string>
            {
                {"H4","FPT3" },
                {"AA4","IPN3" },
            };
                var dic4 = new Dictionary<string, string>
            {
                {"H5","FPT4" },
                {"AA5","IPN4" },
            };
                var data = new List<Dictionary<string, string>>
                {
                    dic1,
                    dic2,
                    dic3,
                    dic4
                };
    
                CreateSpreadsheetWorkbook(@"J:\OpenXMLApp\Myfile.xlsx", data);
            }
    
            public static void CreateSpreadsheetWorkbook(string filepath, List<Dictionary<string, string>> Data)
            {
                // Create a spreadsheet document by supplying the filepath.
                // By default, AutoSave = true, Editable = true, and Type = xlsx.
                var spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
    
                // Add a WorkbookPart to the document.
                var workbookpart = spreadsheetDocument.AddWorkbookPart();
                workbookpart.Workbook = new Workbook();
    
                // Add Sheets to the Workbook.
                var sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets());
    
                // Add a WorksheetPart to the WorkbookPart.
                var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                // Get the sheetData cell table.
                var sheetData = new SheetData();
                worksheetPart.Worksheet = new Worksheet();
    
                var rowindex = 2;
                foreach (var _row in Data)
                {
                    var row = new Row
                    {
                        RowIndex = (uint)rowindex
                    };
                    foreach (var pair in _row)
                    {
                        var newCell = new Cell { CellReference = pair.Key, DataType = CellValues.InlineString };
                        var inlineString = new InlineString();
                        var t = new Text
                        {
                            Text = pair.Value
                        };
                        inlineString.AppendChild(t);
                        newCell.AppendChild(inlineString);
                        row.AppendChild(newCell);
                    }
                    sheetData.AppendChild(row);
                    rowindex++;
                }
    
                worksheetPart.Worksheet.Append(sheetData);
    
                // Append a new worksheet and associate it with the workbook.
                var sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
                sheets.Append(sheet);
    
                spreadsheetDocument.Save();
                // Close the document.
                spreadsheetDocument.Close();
            }
        }
    

    【讨论】:

    • 我仍然看到与此代码相同的问题..:(
    • 这很奇怪,它在目标框架 4.7.2 和 DocumentFormat.OpenXml nuget 版本 2.9.1 上运行良好,并在 Excel 2013 中打开 xlsx 文件。你的环境是什么样的?您是否按原样运行代码而不做任何更改?
    • 是的..我复制粘贴了您的代码以查看它是否有效...我有 .net 4.7 而 OpenXML 是 &lt;package id="DocumentFormat.OpenXml" version="2.9.1" targetFramework="net47" /&gt; 我正在 Office365 中打开 Excel 文件。我在 Excel 2013 和 2010 中尝试过,但仍然遇到同样的问题
    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多