Large Data OpenDocument Writer Example
After Much Trial and Error, and using this Example I found the SheetViews Goes inside the Worksheet at the top. In My case to write large amounts of data I use a streaming and writer approach. I place this here because it's where Google landed me for others.
Freeze First Column Example
using (var workbook = SpreadsheetDocument.Create(uniqueFilePath, SpreadsheetDocumentType.Workbook))
{
List<OpenXmlAttribute> attributeList;
OpenXmlWriter writer;
workbook.AddWorkbookPart();
var workSheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
writer = OpenXmlWriter.Create(workSheetPart);
/* <Worksheet> */
writer.WriteStartElement(new Worksheet());
/*<SheetViews>*/
writer.WriteStartElement(new SheetViews());
var tabSelectedAtt = new OpenXmlAttribute("tabSelected", null, 1.ToString());
var workBookViewIdAtt = new OpenXmlAttribute("workbookViewId", null, 0.ToString());
writer.WriteStartElement(new SheetView(), new List <OpenXmlAttribute>()
{
tabSelectedAtt, workBookViewIdAtt
});
var xSplitAtt = new OpenXmlAttribute("xSplit", null, 1.ToString());
var topLeftCellAtt = new OpenXmlAttribute("topLeftCell", null, "B1");
var activePane = new OpenXmlAttribute("activePane", null, "topRight");
var state = new OpenXmlAttribute("state", null, "frozen");
writer.WriteStartElement(new Pane(), new List <OpenXmlAttribute>()
{
xSplitAtt, topLeftCellAtt, activePane, state
});
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
/*</SheetViews>*/
/* <Columns> */
writer.WriteStartElement(new Columns());
int columnsCounter = 1;
var columnData = reader.GetColumnSchema();
foreach (var c in columnData) {
/* <Column> */
attributeList = new List<OpenXmlAttribute>();
attributeList.Add(new OpenXmlAttribute("min", null, columnsCounter.ToString()));
attributeList.Add(new OpenXmlAttribute("max", null, columnsCounter.ToString()));
attributeList.Add(new OpenXmlAttribute("width", null, ((150 /*Width*/ / 6) + 2).ToString())); //attributeList.Add(new OpenXmlAttribute("width", null, ((col.Width / 6) + 2).ToString()));
writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Column(), attributeList);
writer.WriteEndElement();
columnsCounter++;
/* </Column> */
}
writer.WriteEndElement();
/* </Columns> */
/* <SheetData> */
writer.WriteStartElement(new SheetData());
//...
Freeze First Row Example
using (var workbook = SpreadsheetDocument.Create(uniqueFilePath, SpreadsheetDocumentType.Workbook))
{
List attributeList;
OpenXmlWriter writer;
workbook.AddWorkbookPart();
var workSheetPart = workbook.WorkbookPart.AddNewPart();
writer = OpenXmlWriter.Create(workSheetPart);
/* */
writer.WriteStartElement(new Worksheet());
/*<SheetViews>*/
writer.WriteStartElement(new SheetViews());
var tabSelectedAtt = new OpenXmlAttribute("tabSelected", null, 1.ToString());
var workBookViewIdAtt = new OpenXmlAttribute("workbookViewId", null, 0.ToString());
writer.WriteStartElement(new SheetView(), new List <OpenXmlAttribute>()
{
tabSelectedAtt, workBookViewIdAtt
});
var xSplitAtt = new OpenXmlAttribute("ySplit", null, 1.ToString());
var topLeftCellAtt = new OpenXmlAttribute("topLeftCell", null, "A2");
var activePane = new OpenXmlAttribute("activePane", null, "bottomLeft");
var state = new OpenXmlAttribute("state", null, "frozen");
writer.WriteStartElement(new Pane(), new List <OpenXmlAttribute>()
{
xSplitAtt, topLeftCellAtt, activePane, state
});
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
/*</SheetViews>*/
/* <Columns> */
writer.WriteStartElement(new Columns());
int columnsCounter = 1;
var columnData = reader.GetColumnSchema();
foreach (var c in columnData) {
/* <Column> */
attributeList = new List<OpenXmlAttribute>();
attributeList.Add(new OpenXmlAttribute("min", null, columnsCounter.ToString()));
attributeList.Add(new OpenXmlAttribute("max", null, columnsCounter.ToString()));
attributeList.Add(new OpenXmlAttribute("width", null, ((150 /*Width*/ / 6) + 2).ToString())); //attributeList.Add(new OpenXmlAttribute("width", null, ((col.Width / 6) + 2).ToString()));
writer.WriteStartElement(new DocumentFormat.OpenXml.Spreadsheet.Column(), attributeList);
writer.WriteEndElement();
columnsCounter++;
/* </Column> */
}
writer.WriteEndElement();
/* </Columns> */
/* <SheetData> */
writer.WriteStartElement(new SheetData());