我想通过使用SAX方法和openxmlwriter合并excel中的单元格。我使用这些方法代替DOM方法是将数千条记录写入excel。
答案 0 :(得分:2)
我正在分享一段代码,可以帮助其他人编写代码以使用OpenXmlWriter(OpenXml SAX)合并单元格
/*OpenXMLWriter to write large data to excel to avoid System.OutOfMemoryException*/
OpenXmlWriter oxw = OpenXmlWriter.Create(wsPart);
oxw.WriteStartElement(new Worksheet());
/*Excel column style */
oxw.WriteStartElement(new Columns());
foreach (var item in gridHeaderList)
{
oxw.WriteStartElement(new Column(), new List<OpenXmlAttribute>()
{ new OpenXmlAttribute("min", null, (item.columnIndex + 1).ToString()),
new OpenXmlAttribute("max", null, (item.columnIndex + 1).ToString()),
new OpenXmlAttribute("width", null, item.columnWidth.ToString())
}
);
oxw.WriteEndElement();
}
oxw.WriteEndElement();
/*End of Columns element node*/
/*Sheet data node*/
oxw.WriteStartElement(new SheetData());
oxw.WriteStartElement(new Row());
foreach (var item in dataList)
{
oxw.WriteElement(new Cell() { CellValue = new CellValue(item.Value), DataType = CellValues.String, StyleIndex = item.styleIndex });
}
oxw.WriteEndElement();
oxw.WriteEndElement();
/*End of sheetdata element node*/
/*Start of MergeCells element node NOTE: Make sure initialize "A6:D6" range cells */
oxw.WriteStartElement(new MergeCells());
oxw.WriteElement(new MergeCell() { Reference = new StringValue("A6:D6") });
oxw.WriteEndElement();
/*End of MergeCells element node*/
oxw.WriteEndElement();
/*End of worksheet element node*/
oxw.Close();