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();
}
}
}