using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; namespace ExcelOpenXmlDefinedNames { class Program { static void Main(string[] args) { string sFile = "ExcelOpenXmlDefinedNames.xlsx"; if (File.Exists(sFile)) { File.Delete(sFile); } try { BuildWorkbook(sFile); Console.WriteLine("Program end"); Console.ReadLine(); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.ReadLine(); } } private static void BuildWorkbook(string filename) { using (SpreadsheetDocument xl = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook)) { WorkbookPart wbp = xl.AddWorkbookPart(); WorksheetPart wsp = wbp.AddNewPart(); Workbook wb = new Workbook(); FileVersion fv = new FileVersion(); fv.ApplicationName = "Microsoft Office Excel"; Worksheet ws = new Worksheet(); SheetData sd = new SheetData(); WorkbookStylesPart wbsp = wbp.AddNewPart(); wbsp.Stylesheet = CreateStylesheet(); wbsp.Stylesheet.Save(); Row r; Cell c; r = new Row(); r.RowIndex = 2; c = new Cell(); c.CellReference = "C2"; c.CellValue = new CellValue("2"); r.Append(c); sd.Append(r); r = new Row(); r.RowIndex = 3; c = new Cell(); c.CellReference = "C3"; c.CellValue = new CellValue("3"); r.Append(c); sd.Append(r); r = new Row(); r.RowIndex = 4; c = new Cell(); c.CellReference = "C4"; c.CellValue = new CellValue("5"); r.Append(c); sd.Append(r); r = new Row(); r.RowIndex = 5; c = new Cell(); c.CellReference = "C5"; c.CellValue = new CellValue("7"); r.Append(c); sd.Append(r); r = new Row(); r.RowIndex = 6; c = new Cell(); c.CellReference = "B6"; c.DataType = CellValues.String; c.CellValue = new CellValue("SUM"); r.Append(c); c = new Cell(); c.CellReference = "C6"; c.CellFormula = new CellFormula("SUM(PrimeNum1, PrimeNum2, PrimeNum3, PrimeNum4)"); c.CellValue = new CellValue("17"); r.Append(c); sd.Append(r); ws.Append(sd); wsp.Worksheet = ws; Sheets sheets = new Sheets(); Sheet sheet = new Sheet(); sheet.Name = "Sheet1"; sheet.SheetId = 1; sheet.Id = wbp.GetIdOfPart(wsp); sheets.Append(sheet); wb.Append(fv); wb.Append(sheets); // DefinedNames has to be appended after Sheets DefinedNames defnames = new DefinedNames(); DefinedName defname; defname = new DefinedName(); defname.Name = "PrimeNum1"; defname.Text = "Sheet1!$C$2"; defnames.Append(defname); defname = new DefinedName(); defname.Name = "PrimeNum2"; defname.Text = "Sheet1!$C$3"; defnames.Append(defname); defname = new DefinedName(); defname.Name = "PrimeNum3"; defname.Text = "Sheet1!$C$4"; defnames.Append(defname); defname = new DefinedName(); defname.Name = "PrimeNum4"; defname.Text = "Sheet1!$C$5"; defnames.Append(defname); wb.Append(defnames); xl.WorkbookPart.Workbook = wb; xl.WorkbookPart.Workbook.Save(); xl.Close(); } } private static Stylesheet CreateStylesheet() { Stylesheet ss = new Stylesheet(); Fonts fts = new Fonts(); DocumentFormat.OpenXml.Spreadsheet.Font ft = new DocumentFormat.OpenXml.Spreadsheet.Font(); FontName ftn = new FontName(); ftn.Val = "Calibri"; FontSize ftsz = new FontSize(); ftsz.Val = 11; ft.FontName = ftn; ft.FontSize = ftsz; fts.Append(ft); fts.Count = (uint)fts.ChildElements.Count; Fills fills = new Fills(); Fill fill; PatternFill patternFill; fill = new Fill(); patternFill = new PatternFill(); patternFill.PatternType = PatternValues.None; fill.PatternFill = patternFill; fills.Append(fill); fill = new Fill(); patternFill = new PatternFill(); patternFill.PatternType = PatternValues.Gray125; fill.PatternFill = patternFill; fills.Append(fill); fills.Count = (uint)fills.ChildElements.Count; Borders borders = new Borders(); Border border = new Border(); border.LeftBorder = new LeftBorder(); border.RightBorder = new RightBorder(); border.TopBorder = new TopBorder(); border.BottomBorder = new BottomBorder(); border.DiagonalBorder = new DiagonalBorder(); borders.Append(border); borders.Count = (uint)borders.ChildElements.Count; CellStyleFormats csfs = new CellStyleFormats(); CellFormat cf = new CellFormat(); cf.NumberFormatId = 0; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; csfs.Append(cf); csfs.Count = (uint)csfs.ChildElements.Count; uint iExcelIndex = 164; NumberingFormats nfs = new NumberingFormats(); CellFormats cfs = new CellFormats(); cf = new CellFormat(); cf.NumberFormatId = 0; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cfs.Append(cf); NumberingFormat nf; nf = new NumberingFormat(); nf.NumberFormatId = iExcelIndex++; nf.FormatCode = "dd/mm/yyyy hh:mm:ss"; nfs.Append(nf); cf = new CellFormat(); cf.NumberFormatId = nf.NumberFormatId; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cfs.Append(cf); nf = new NumberingFormat(); nf.NumberFormatId = iExcelIndex++; nf.FormatCode = "#,##0.0000"; nfs.Append(nf); cf = new CellFormat(); cf.NumberFormatId = nf.NumberFormatId; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cfs.Append(cf); // #,##0.00 is also Excel style index 4 nf = new NumberingFormat(); nf.NumberFormatId = iExcelIndex++; nf.FormatCode = "#,##0.00"; nfs.Append(nf); cf = new CellFormat(); cf.NumberFormatId = nf.NumberFormatId; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cfs.Append(cf); // @ is also Excel style index 49 nf = new NumberingFormat(); nf.NumberFormatId = iExcelIndex++; nf.FormatCode = "@"; nfs.Append(nf); cf = new CellFormat(); cf.NumberFormatId = nf.NumberFormatId; cf.FontId = 0; cf.FillId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cfs.Append(cf); nfs.Count = (uint)nfs.ChildElements.Count; cfs.Count = (uint)cfs.ChildElements.Count; ss.Append(nfs); ss.Append(fts); ss.Append(fills); ss.Append(borders); ss.Append(csfs); ss.Append(cfs); CellStyles css = new CellStyles(); CellStyle cs = new CellStyle(); cs.Name = "Normal"; cs.FormatId = 0; cs.BuiltinId = 0; css.Append(cs); css.Count = (uint)css.ChildElements.Count; ss.Append(css); DifferentialFormats dfs = new DifferentialFormats(); dfs.Count = 0; ss.Append(dfs); TableStyles tss = new TableStyles(); tss.Count = 0; tss.DefaultTableStyle = "TableStyleMedium9"; tss.DefaultPivotStyle = "PivotStyleLight16"; ss.Append(tss); return ss; } } }