0

尝试使用 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.
    }
}
4

1 回答 1

0

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

原来我一直在使用属性 ss:ExpandedRowCount,并将其设置为 2000。在记录数超过此数字的情况下,XML 电子表格文档被视为无效。一旦我删除了这个可选属性,生成的报告就可以正常工作。

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

于 2012-09-03T13:16:53.733 回答