add_cell_string(Row row, SharedStringStore strings, string str) { uint col_index = (uint)row.ChildElements.Count(); uint row_index = row.RowIndex; Cell cell = new Cell() { DataType = CellValues.SharedString, CellValue = new CellValue(strings.putString(str).ToString()), CellReference = Char.ConvertFromUtf32((int)'A' + (int)col_index) + row_index }; row.AppendChild <Cell>(cell); }
public void Save(string file) { using (SpreadsheetDocument doc = SpreadsheetDocument.Create(file, SpreadsheetDocumentType.Workbook)) // Create new workbook { SharedStringStore strings = new SharedStringStore(); WorkbookPart wbp = doc.AddWorkbookPart(); wbp.Workbook = new Workbook(); WorksheetPart wsp = wbp.AddNewPart <WorksheetPart>(); SheetData sheet_data = new SheetData(); wsp.Worksheet = new Worksheet(sheet_data); Sheets sheets = wbp.Workbook.AppendChild <Sheets>(new Sheets()); Sheet sheet = new Sheet() { Id = wbp.GetIdOfPart(wsp), SheetId = 1, Name = "Alarms" }; sheets.Append(sheet); uint row_index = 1; // Header row Row row = new Row() { RowIndex = new UInt32Value(row_index) }; // ID add_cell_string(row, strings, "ID"); // Name add_cell_string(row, strings, "Name"); // Text add_cell_string(row, strings, "Text"); // Options add_cell_string(row, strings, "Options"); sheet_data.AppendChild <Row>(row); row_index++; foreach (AlarmDefinition d in this) { row = new Row() { RowIndex = new UInt32Value(row_index) }; // ID add_cell_int(row, d.ID); // Name add_cell_string(row, strings, d.Name); // Text add_cell_string(row, strings, d.Text); // Options add_cell_string(row, strings, GetOptionString(d.Options)); sheet_data.AppendChild <Row>(row); row_index++; } strings.buildTable(doc); wbp.Workbook.Save(); } }