该文件已损坏,无法打开,C#,XmlWriter

时间:2012-08-30 14:55:54

标签: c# xmlwriter

尝试使用XmlWriter创建XML报告。 奇怪的是,对于大量记录,我在尝试使用MS Excel打开报告文件时出现以下错误:“文件已损坏且无法打开”。但是,我认为报告文件没有损坏,与其他编辑器(例如记事本)一样,我可以打开文件并确保内容正常。

在下面的代码中,循环在1995年的上限下工作正常,但是当上限为1996时失败。

提前致谢,

=============================================== =========================

    private static void WriteReport(XmlWriter writer, ReportingFilters filters, DataContext context)
    {

        writer.WriteStartElement("Table"); // Begin Table

        writer.WriteAttributeString("ss", "ExpandedColumnCount", null, "7");
        writer.WriteAttributeString("ss", "ExpandedRowCount", null, "2000");
        writer.WriteAttributeString("x", "FullColumns", null, "1");
        writer.WriteAttributeString("x", "FullRows", null, "1");

        writer.WriteStartElement("Column");
        writer.WriteAttributeString("ss", "Width", null, "200");
        writer.WriteEndElement();

        writer.WriteStartElement("Column");
        writer.WriteAttributeString("ss", "Width", null, "125");
        writer.WriteEndElement();

        writer.WriteStartElement("Column");
        writer.WriteAttributeString("ss", "Width", null, "100");
        writer.WriteEndElement();

        writer.WriteStartElement("Column");
        writer.WriteAttributeString("ss", "Width", null, "75");
        writer.WriteEndElement();

        writer.WriteStartElement("Column");
        writer.WriteAttributeString("ss", "Width", null, "75");
        writer.WriteEndElement();

        // Write the title.
        writer.WriteStartElement("Row");
        writer.WriteStartElement("Cell");
        writer.WriteAttributeString("ss", "StyleID", null, "BorderedTitle");
        writer.WriteStartElement("Data");
        writer.WriteAttributeString("ss", "Type", null, "String");
        writer.WriteString("Selected Dates: " + filters.StartDate.ToShortDateString() + " -" + filters.EndDate.ToShortDateString());
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndElement();

        // Write the title.
        writer.WriteStartElement("Row");
        writer.WriteStartElement("Cell");
        writer.WriteAttributeString("ss", "StyleID", null, "BorderedTitle");
        writer.WriteStartElement("Data");
        writer.WriteAttributeString("ss", "Type", null, "String");
        writer.WriteString("Date Report was Run: " + DateTime.Now.ToShortDateString());
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndElement();

        //.. etc

        var reportResults = ReportingManager.GetHospitalHitRate(context, filters);
        var totalDemos = 0;
        var totalMatched = 0;

            // Loop works OK for 1995, 1996 fails with "The file is corrupt and cannot be opened"
            for (int m = 0; m < 1995; m++ )
            {
                writer.WriteStartElement("Row"); // Begin Content Row

                writer.WriteStartElement("Cell");
                writer.WriteStartElement("Data");
                writer.WriteAttributeString("ss", "Type", null, "String");
                writer.WriteString("aaaaaaaaaaaaaaaaaaaaaaa");
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("Cell");
                writer.WriteStartElement("Data");
                writer.WriteAttributeString("ss", "Type", null, "String");
                writer.WriteString("bbbbbbbbbbbbbbb");
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("Cell");
                writer.WriteStartElement("Data");
                writer.WriteAttributeString("ss", "Type", null, "String");
                writer.WriteString("cccccccccccc");
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("Cell");
                writer.WriteStartElement("Data");
                writer.WriteAttributeString("ss", "Type", null, "String");
                writer.WriteString("dddddddddd");
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("Cell");
                writer.WriteStartElement("Data");
                writer.WriteAttributeString("ss", "Type", null, "String");

                //.. etc
                writer.WriteString("%");
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteEndElement();

            }

        //.. etc

        writer.WriteEndElement(); // End Table.
    }
}

1 个答案:

答案 0 :(得分:0)

经过一些谷歌搜索后,我找到了“XML电子表格参考” http://msdn.microsoft.com/en-us/library/office/aa140066%28v=office.10%29.aspx

原来我一直在使用属性ss:ExpandedRowCount,我把它设置为2000。 在记录数超过此数量的情况下,XML Spreadsheet文档被视为无效。 删除此可选属性后,生成的报告工作正常。

属性:ss:ExpandedRowCount 描述:指定此表中的总行数,而不考虑稀疏性。如果指定的行和列已扩展为完整大小,则此属性定义表的总大小。如果指定,则此属性必须与表同步。表中的行索引应从1开始并转到ExpandedRowCount。如果此值与表不同步,则指定的XML电子表格文档无效。