一般问题是您不应该将 DOM 和 SAX 方法混合在一起。一旦混合它们,性能就类似于仅使用 DOM。当您全力以赴时,SAX 的性能优势就会显现出来。首先回答您的问题:
有没有人找到一种方法来提高作家或设置一种写作方式
次数少一些?有没有方法可以加速这个过程?
不要将 SAX 编写器与 DOM 操作混合在一起。这意味着您根本不应该对 SDK 类属性或函数进行操作。所以 cell.Append() 已经过时了。 cell.DataType 或 cell.StyleIndex 也是如此。
当你做 SAX 时,你会全力以赴。(这听起来有点挑衅......)例如:
for (int i = 1; i <= 50000; ++i)
{
oxa = new List<OpenXmlAttribute>();
// this is the row index
oxa.Add(new OpenXmlAttribute("r", null, i.ToString()));
oxw.WriteStartElement(new Row(), oxa);
for (int j = 1; j <= 100; ++j)
{
oxa = new List<OpenXmlAttribute>();
// this is the data type ("t"), with CellValues.String ("str")
oxa.Add(new OpenXmlAttribute("t", null, "str"));
// it's suggested you also have the cell reference, but
// you'll have to calculate the correct cell reference yourself.
// Here's an example:
//oxa.Add(new OpenXmlAttribute("r", null, "A1"));
oxw.WriteStartElement(new Cell(), oxa);
oxw.WriteElement(new CellValue(string.Format("R{0}C{1}", i, j)));
// this is for Cell
oxw.WriteEndElement();
}
// this is for Row
oxw.WriteEndElement();
}
其中 oxa 是一个 List,oxw 是 SAX writer 类 OpenXmlWriter。更多详情请看我的文章here.
没有真正的方法来缓存 SAX 操作。它们就像一系列 printf 语句。您可以编写一个辅助函数,只在块中执行 WriteStartElement()、WriteElement() 和 WriteEndElement() 函数(例如,编写完整的 Cell 类)。