Advertisement
Perseus

OpenXml

Sep 7th, 2020
1,501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using DocumentFormat.OpenXml;
  7. using DocumentFormat.OpenXml.Packaging;
  8. using DocumentFormat.OpenXml.Spreadsheet;
  9.  
  10. namespace OpenXMLSample
  11. {
  12.     public class StyleSheetFormats
  13.     {
  14.         public void FormatStyleSheet(string fileName)
  15.         {
  16.             using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
  17.             {
  18.                 WorkbookPart workbookPart = document.AddWorkbookPart();
  19.                 workbookPart.Workbook = new Workbook();
  20.  
  21.                 WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
  22.                 worksheetPart.Worksheet = new Worksheet();
  23.  
  24.                 // Adding style
  25.                 WorkbookStylesPart stylePart = workbookPart.AddNewPart<WorkbookStylesPart>();
  26.                 stylePart.Stylesheet = GenerateStylesheet();
  27.                 stylePart.Stylesheet.Save();
  28.  
  29.                 // Setting up columns
  30.                 Columns columns = new Columns(
  31.                         new Column // hours column
  32.                         {
  33.                             Min = 1,
  34.                             Max = 1,
  35.                             Width = 4,
  36.                             CustomWidth = true
  37.                         },
  38.                         new Column // day/hours
  39.                         {
  40.                             Min = 2,
  41.                             Max = 180, // maxColumnCount
  42.                             Width = 2.4,
  43.                             CustomWidth = true
  44.                         });
  45.  
  46.                 worksheetPart.Worksheet.AppendChild(columns);
  47.  
  48.                 Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
  49.  
  50.                 Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Employees" };
  51.  
  52.                 sheets.Append(sheet);
  53.  
  54.                 workbookPart.Workbook.Save();
  55.  
  56.                 List<Employee> employees = Employees.EmployeesList;
  57.  
  58.                 SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
  59.  
  60.                 // Constructing header
  61.                 Row row = new Row();
  62.  
  63.                 row.Append(
  64.                     ConstructCell("Id", CellValues.String, 2),
  65.                     ConstructCell("Name", CellValues.String, 2),
  66.                     ConstructCell("Birth Date", CellValues.String, 2),
  67.                     ConstructCell("Salary", CellValues.String, 2));
  68.  
  69.                 // Insert the header row to the Sheet Data
  70.                 sheetData.AppendChild(row);
  71.  
  72.                 // Inserting each employee
  73.                 foreach (var employee in employees)
  74.                 {
  75.                     row = new Row();
  76.  
  77.                     row.Append(
  78.                         ConstructCell(employee.Id.ToString(), CellValues.Number, 1),
  79.                         ConstructCell(employee.Name, CellValues.String, 1),
  80.                         ConstructCell(employee.DOB.ToString("yyyy/MM/dd"), CellValues.String, 1),
  81.                         ConstructCell(employee.Salary.ToString(), CellValues.Number, 1));
  82.  
  83.                     sheetData.AppendChild(row);
  84.                 }
  85.  
  86.                 worksheetPart.Worksheet.Save();
  87.             }
  88.         }
  89.  
  90.         private Cell ConstructCell(string value, CellValues dataType, uint styleIndex = 0)
  91.         {
  92.             return new Cell()
  93.             {
  94.                 CellValue = new CellValue(value),
  95.                 DataType = new EnumValue<CellValues>(dataType),
  96.                 StyleIndex = styleIndex
  97.             };
  98.         }
  99.  
  100.         private Stylesheet GenerateStylesheet()
  101.         {
  102.             Stylesheet styleSheet = null;
  103.  
  104.             Fonts fonts = new Fonts(
  105.                 new Font( // Index 0 - default
  106.                     new FontSize() { Val = 10 }
  107.  
  108.                 ),
  109.                  new Font( // Index 1 - header
  110.                     new FontSize() { Val = 12 },
  111.                     new Bold(),
  112.                     new Color() { Rgb = "FFFFFF" },
  113.                     new FontName() { Val = "Calibri" }
  114.                 ),
  115.                 new Font( // Index 2 - cell innerText
  116.                     new FontSize() { Val = 10 },
  117.                     new Color() { Rgb = "FFFFFF" },
  118.                     new FontName() { Val = "Calibri" }
  119.                 ));
  120.  
  121.             Fills fills = new Fills(
  122.                     new Fill(new PatternFill() { PatternType = PatternValues.None }), // Index 0 - default
  123.                     new Fill(new PatternFill(new ForegroundColor { Rgb = new HexBinaryValue() { Value = "C6E0B4" } })
  124.                     { PatternType = PatternValues.Solid }), // Index 1 - primary row
  125.                     new Fill(new PatternFill(new ForegroundColor { Rgb = new HexBinaryValue() { Value = "FFE699" } })
  126.                     { PatternType = PatternValues.Solid }) // Index 2 - candle
  127.                 );
  128.  
  129.             Borders borders = new Borders(
  130.                 new Border(
  131.                     new LeftBorder(),
  132.                     new RightBorder(),
  133.                     new TopBorder(),
  134.                     new BottomBorder(),
  135.                     new DiagonalBorder()), // index 0 - default
  136.                 new Border( // index 1 primary row
  137.                     new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None },
  138.                     new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None },
  139.                     new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None },
  140.                     new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Dotted },
  141.                     new DiagonalBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None }),
  142.                 new Border( // index 2 one cell candle black border
  143.                     new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  144.                     new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  145.                     new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  146.                     new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  147.                     new DiagonalBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None }),
  148.                 new Border( // index 3 middle candle cell black border
  149.                     new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  150.                     new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  151.                     new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None },
  152.                     new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None },
  153.                     new DiagonalBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None }),
  154.                 new Border( // index 4 top candle cell black border
  155.                     new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  156.                     new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  157.                     new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  158.                     new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None },
  159.                     new DiagonalBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None }),
  160.                 new Border( // index 5 bottom candle cell black border
  161.                     new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  162.                     new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  163.                     new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
  164.                     new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None },
  165.                     new DiagonalBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None }),
  166.                 new Border( // index 6 header cell gray border
  167.                     new LeftBorder(new Color() { Rgb = new HexBinaryValue() { Value = "808080" } }) { Style = BorderStyleValues.Thin },
  168.                     new RightBorder(new Color() { Rgb = new HexBinaryValue() { Value = "808080" } }) { Style = BorderStyleValues.Thin },
  169.                     new TopBorder(new Color() { Rgb = new HexBinaryValue() { Value = "808080" } }) { Style = BorderStyleValues.Thin },
  170.                     new BottomBorder(new Color() { Rgb = new HexBinaryValue() { Value = "808080" } }) { Style = BorderStyleValues.Thin },
  171.                     new DiagonalBorder(new Color() { Auto = true }) { Style = BorderStyleValues.None })
  172.                 );
  173.  
  174.             CellFormats cellFormats = new CellFormats(
  175.                 new CellFormat(), // default
  176.                 new CellFormat { FontId = 1, FillId = 0, BorderId = 6, ApplyFont = true, ApplyBorder = true }, // header
  177.                 new CellFormat { FontId = 0, FillId = 1, BorderId = 1, ApplyBorder = true, ApplyFont = true, ApplyFill = true }, // primary row cell
  178.                 new CellFormat { FontId = 2, FillId = 2, BorderId = 2, ApplyBorder = true, ApplyFont = true, ApplyFill = true }, // one candle cell
  179.                 new CellFormat { FontId = 2, FillId = 2, BorderId = 3, ApplyBorder = true, ApplyFont = true, ApplyFill = true }, // middle candle cell
  180.                 new CellFormat { FontId = 2, FillId = 2, BorderId = 4, ApplyBorder = true, ApplyFont = true, ApplyFill = true }, // top candle cell
  181.                 new CellFormat { FontId = 2, FillId = 2, BorderId = 5, ApplyBorder = true, ApplyFont = true, ApplyFill = true }  // bottom candle cell
  182.                 );
  183.  
  184.             styleSheet = new Stylesheet(fonts, fills, borders, cellFormats);
  185.  
  186.             return styleSheet;
  187.         }
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement