I am creating a large XLSX file from a datatable using the SAX method suggested in Parsing and reading large Excel files using the Open XML SDK . I am using an xlsx file as a template.
The method described in this post works fine to replace a new sheet with an existing one, but I want to copy the title bar from the sheet in the template (string values, formatting, etc.) instead of just using the title bar from datatable, like this makes the source code.
I tried the code below, but the XLSX file does not contain text in the title bar - formatting is copied, not text. I looked at the XML file for the worksheet and it looks good to me (link to the sharedStrings.xml file, which still contains the string definition). The reflected code from the Open XML SDK 2.0 Productivity Tool shows a slightly strange result: the cells have no text value:
cellValue1.Text = "";
although the XML says:
<x:cr="A1" s="4" t="s">
The main code used by OpenXmlReader is given 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(); } } }
source share