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