Created
January 12, 2023 11:13
-
-
Save devmnj/f3ec6d56a7b6e09ae0cc147a39e0ae10 to your computer and use it in GitHub Desktop.
Create a spread sheet with custom styles using Open XML DSK
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using (SpreadsheetDocument document = SpreadsheetDocument.Create("somefile.xls", SpreadsheetDocumentType.Workbook)) | |
| { | |
| WorkbookPart workbookPart = document.AddWorkbookPart(); | |
| workbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook(); | |
| WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(); | |
| WorkbookStylesPart sp = workbookPart.AddNewPart<WorkbookStylesPart>(); | |
| sp.Stylesheet = new DocumentFormat.OpenXml.Spreadsheet.Stylesheet(); | |
| Stylesheet stylesheet = sp.Stylesheet; | |
| stylesheet.Fonts = new DocumentFormat.OpenXml.Spreadsheet.Fonts(); | |
| stylesheet.CellFormats = new DocumentFormat.OpenXml.Spreadsheet.CellFormats(); | |
| Font font = new Font(new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 11 }, new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = HexBinaryValue.FromString("4b53cb") }, new DocumentFormat.OpenXml.Spreadsheet.Bold()); | |
| Font font2 = new Font(new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 15 }, new DocumentFormat.OpenXml.Spreadsheet.Color() { Rgb = HexBinaryValue.FromString("c93653") }, new DocumentFormat.OpenXml.Spreadsheet.Italic()); | |
| stylesheet.Fonts.Append(font); | |
| stylesheet.Fonts.Append(font2); | |
| DocumentFormat.OpenXml.Spreadsheet.Fill fill = new DocumentFormat.OpenXml.Spreadsheet.Fill() { }; | |
| DocumentFormat.OpenXml.Spreadsheet.PatternFill patternFill = new DocumentFormat.OpenXml.Spreadsheet.PatternFill() { PatternType = PatternValues.Solid }; | |
| DocumentFormat.OpenXml.Spreadsheet.BackgroundColor backgroundColor = new DocumentFormat.OpenXml.Spreadsheet.BackgroundColor() { Rgb = HexBinaryValue.FromString("ed5976") }; | |
| DocumentFormat.OpenXml.Spreadsheet.ForegroundColor foregroundColor = new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor() { Rgb = HexBinaryValue.FromString("ed5976") }; | |
| patternFill.Append(backgroundColor); | |
| patternFill.Append(foregroundColor); | |
| fill.Append(patternFill); | |
| stylesheet.Fills = new Fills(); | |
| stylesheet.Fills.AppendChild(fill); | |
| DocumentFormat.OpenXml.Spreadsheet.Border border2 = new DocumentFormat.OpenXml.Spreadsheet.Border(); | |
| DocumentFormat.OpenXml.Spreadsheet.LeftBorder leftBorder2 = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder() { Style = BorderStyleValues.Thin }; | |
| Color color1 = new Color() { Indexed = (UInt32Value)64U }; | |
| leftBorder2.Append(color1); | |
| RightBorder rightBorder2 = new RightBorder() { Style = BorderStyleValues.Thin }; | |
| Color color2 = new Color() { Indexed = (UInt32Value)64U }; | |
| rightBorder2.Append(color2); | |
| DocumentFormat.OpenXml.Spreadsheet.TopBorder topBorder2 = new DocumentFormat.OpenXml.Spreadsheet.TopBorder() { Style = BorderStyleValues.Thin }; | |
| Color color3 = new Color() { Indexed = (UInt32Value)64U }; | |
| topBorder2.Append(color3); | |
| DocumentFormat.OpenXml.Spreadsheet.BottomBorder bottomBorder2 = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder() { Style = BorderStyleValues.Thin }; | |
| Color color4 = new Color() { Indexed = (UInt32Value)64U }; | |
| bottomBorder2.Append(color4); | |
| DiagonalBorder diagonalBorder2 = new DiagonalBorder(); | |
| border2.Append(leftBorder2); | |
| border2.Append(rightBorder2); | |
| border2.Append(topBorder2); | |
| border2.Append(bottomBorder2); | |
| border2.Append(diagonalBorder2); | |
| stylesheet.Borders = new Borders(); | |
| stylesheet.Borders.Append(border2); | |
| CellFormat cellFormat1 = new CellFormat() { FontId = 0, FillId = 0, BorderId = 0 }; | |
| CellFormat cellFormat2 = new CellFormat() { FontId = 1, FillId = 0 }; | |
| CellFormat cellFormat3 = new CellFormat(new Alignment() { Horizontal = HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) { FontId = 1, FillId = 0 }; | |
| stylesheet.CellFormats.Append(cellFormat1); | |
| stylesheet.CellFormats.Append(cellFormat2); | |
| stylesheet.CellFormats.Append(cellFormat3); | |
| stylesheet.Save(); | |
| SheetData sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData(); | |
| MergeCells mergeCells = new MergeCells(); | |
| workRow.Append(CreateCell("Expenses", 0)); | |
| sheetData.Append(workRow); | |
| worksheetPart.Worksheet = new Worksheet(sheetData); | |
| worksheetPart.Worksheet.Save(); | |
| Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); | |
| Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Trial" }; | |
| sheets.Append(sheet); | |
| workbookPart.Workbook.Save(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment