将模板与 OpenXML 和 SAX 结合使用
我正在使用 使用 Open XML SDK 解析和读取大型 Excel 文件。我使用 XLSX 文件作为模板。
该帖子中描述的方法可以很好地用新工作表替换现有工作表,但我想从模板中的工作表复制标题行(字符串值、格式等),而不是仅使用来自的标题行数据表与原始代码一样。
我已经尝试了下面的代码,但 XLSX 文件最终在标题行中没有文本 - 格式被复制,只是没有复制文本。我查看了该工作表的 XML 文件,对我来说看起来没问题(引用sharedStrings.xml 文件,该文件仍然具有字符串的定义)。 Open XML SDK 2.0 Productivity Tool 中的反映代码显示了但结果有点奇怪:单元格似乎没有设置文本值:
cellValue1.Text = "";
即使 XML 说:
<x:c r="A1" s="4" t="s">
OpenXmlReader 使用的主要代码如下:
while (reader.Read())
{
if (reader.ElementType == typeof(SheetData))
{
if (reader.IsEndElement)
continue;
// Write sheet element
writer.WriteStartElement(new SheetData());
// copy header row from template
reader.Read();
do
{
if (reader.IsStartElement)
{
writer.WriteStartElement(reader);
}
else if (reader.IsEndElement)
{
writer.WriteEndElement();
}
reader.Read();
} while (!(reader.ElementType == typeof(Row) && reader.IsEndElement));
writer.WriteEndElement();
// Write data rows
foreach (DataRow dataRow in resultsTable.Rows)
{
// Write row element
Row r = new Row();
writer.WriteStartElement(r);
foreach (DataColumn dataCol in resultsTable.Columns)
{
Cell c = new Cell();
c.DataType = CellValues.String;
CellValue v = new CellValue(dataRow[dataCol].ToString());
c.Append(v);
// Write cell element
writer.WriteElement(c);
}
// End row
writer.WriteEndElement();
}
// End sheet
writer.WriteEndElement();
}
else
{
if (reader.IsStartElement)
{
writer.WriteStartElement(reader);
}
else if (reader.IsEndElement)
{
writer.WriteEndElement();
}
}
}
I'm creating a large XLSX file from a datatable, using the SAX method proposed in Parsing and Reading Large Excel Files with the Open XML SDK. I'm using an XLSX file as a template.
The method described in that post works fine to substitute a new sheet in for an existing one, but I want to copy the header row from the sheet in the template (string values, formatting, etc), instead of just using the header row from the datatable as the original code does.
I've tried the code below, but the XLSX file ends up with no text in the header row - the formatting is copied, just not the text. I've looked in the XML file for the sheet and it looks OK to me (referencing the sharedStrings.xml file, which still has the definition of the strings). The reflected code from the Open XML SDK 2.0 Productivity Tool shows a slightly odd result though: the cells don't appear to have a text value set:
cellValue1.Text = "";
even though the XML says:
<x:c r="A1" s="4" t="s">
The main code used by the OpenXmlReader is below:
while (reader.Read())
{
if (reader.ElementType == typeof(SheetData))
{
if (reader.IsEndElement)
continue;
// Write sheet element
writer.WriteStartElement(new SheetData());
// copy header row from template
reader.Read();
do
{
if (reader.IsStartElement)
{
writer.WriteStartElement(reader);
}
else if (reader.IsEndElement)
{
writer.WriteEndElement();
}
reader.Read();
} while (!(reader.ElementType == typeof(Row) && reader.IsEndElement));
writer.WriteEndElement();
// Write data rows
foreach (DataRow dataRow in resultsTable.Rows)
{
// Write row element
Row r = new Row();
writer.WriteStartElement(r);
foreach (DataColumn dataCol in resultsTable.Columns)
{
Cell c = new Cell();
c.DataType = CellValues.String;
CellValue v = new CellValue(dataRow[dataCol].ToString());
c.Append(v);
// Write cell element
writer.WriteElement(c);
}
// End row
writer.WriteEndElement();
}
// End sheet
writer.WriteEndElement();
}
else
{
if (reader.IsStartElement)
{
writer.WriteStartElement(reader);
}
else if (reader.IsEndElement)
{
writer.WriteEndElement();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
线索是生产力工具在生成的工作表上显示标题单元格的空白值,并且模板中的验证公式也丢失。这些都是文本,未使用
OpenXmlReader.Read()
和OpenXmlReader.WriteStartElement()
组合从模板工作表复制到新工作表。当元素是
OpenXmlLeafTextElement
< /a> 然后OpenXmlReader.GetText()
方法将返回文本 - 这适用于单元格中的文本值和公式。工作代码如下所示:
The clue was that the Productivity Tool was showing blank values for the header cells on the generated sheet, and also that the validation formulae from the template were missing. These are both text, which wasn't copied from the template sheet to the new sheet using the combination of
OpenXmlReader.Read()
andOpenXmlReader.WriteStartElement()
.When the element is an
OpenXmlLeafTextElement
then theOpenXmlReader.GetText()
method will return the text - this works for both text values in cells and for formulae.The working code is shown below: