|
| 1 | +using DocumentFormat.OpenXml; |
| 2 | +using DocumentFormat.OpenXml.Packaging; |
| 3 | +using DocumentFormat.OpenXml.Spreadsheet; |
| 4 | +using System; |
| 5 | + |
| 6 | +public class StyleUtility |
| 7 | +{ |
| 8 | + private readonly WorkbookStylesPart stylesPart; |
| 9 | + private readonly Stylesheet stylesheet; |
| 10 | + |
| 11 | + public uint CreateDefaultStyle() |
| 12 | + { |
| 13 | + return CreateStyle("Arial", 12, "000000"); |
| 14 | + } |
| 15 | + |
| 16 | + public StyleUtility(WorkbookStylesPart stylesPart) |
| 17 | + { |
| 18 | + this.stylesPart = stylesPart ?? throw new ArgumentNullException(nameof(stylesPart)); |
| 19 | + this.stylesheet = this.stylesPart.Stylesheet ?? new Stylesheet(); |
| 20 | + |
| 21 | + // Initialize Fonts, Fills, Borders, and CellFormats if they are null |
| 22 | + this.stylesheet.Fonts = this.stylesheet.Fonts ?? new Fonts(); |
| 23 | + this.stylesheet.Fills = this.stylesheet.Fills ?? new Fills(); |
| 24 | + this.stylesheet.Borders = this.stylesheet.Borders ?? new Borders(); |
| 25 | + this.stylesheet.CellFormats = this.stylesheet.CellFormats ?? new CellFormats(); |
| 26 | + } |
| 27 | + |
| 28 | + public uint CreateStyle(string fontName, double fontSize, string hexColor) |
| 29 | + { |
| 30 | + // Validate inputs |
| 31 | + if (string.IsNullOrEmpty(fontName)) |
| 32 | + throw new ArgumentNullException(nameof(fontName)); |
| 33 | + if (fontSize <= 0) |
| 34 | + throw new ArgumentOutOfRangeException(nameof(fontSize), "Font size must be greater than zero"); |
| 35 | + if (string.IsNullOrEmpty(hexColor) || !IsHexColor(hexColor)) |
| 36 | + throw new ArgumentException("Invalid hex color", nameof(hexColor)); |
| 37 | + |
| 38 | + // Create and append Font |
| 39 | + var font = new Font( |
| 40 | + new FontSize() { Val = fontSize }, |
| 41 | + new Color() { Rgb = new HexBinaryValue() { Value = hexColor } }, |
| 42 | + new FontName() { Val = fontName } |
| 43 | + ); |
| 44 | + this.stylesheet.Fonts.Append(font); |
| 45 | + |
| 46 | + // Create and append Fill |
| 47 | + var fill = new Fill(new PatternFill() { PatternType = PatternValues.None }); |
| 48 | + this.stylesheet.Fills.Append(fill); |
| 49 | + |
| 50 | + // Create and append Border |
| 51 | + var border = new Border(new LeftBorder(), new RightBorder(), new TopBorder(), new BottomBorder(), new DiagonalBorder()); |
| 52 | + this.stylesheet.Borders.Append(border); |
| 53 | + |
| 54 | + // Create and append CellFormat |
| 55 | + var cellFormat = new CellFormat |
| 56 | + { |
| 57 | + FontId = new UInt32Value((uint)this.stylesheet.Fonts.ChildElements.Count - 1), |
| 58 | + FillId = new UInt32Value((uint)this.stylesheet.Fills.ChildElements.Count - 1), |
| 59 | + BorderId = new UInt32Value((uint)this.stylesheet.Borders.ChildElements.Count - 1) |
| 60 | + }; |
| 61 | + this.stylesheet.CellFormats.Append(cellFormat); |
| 62 | + |
| 63 | + return (uint)this.stylesheet.CellFormats.ChildElements.Count - 1; |
| 64 | + } |
| 65 | + |
| 66 | + private static bool IsHexColor(string color) |
| 67 | + { |
| 68 | + return System.Text.RegularExpressions.Regex.IsMatch(color, "^(#)?([0-9a-fA-F]{3})([0-9a-fA-F]{3})?$"); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + public void SaveStylesheet() |
| 73 | + { |
| 74 | + if (this.stylesPart.Stylesheet == null) |
| 75 | + { |
| 76 | + this.stylesPart.Stylesheet = this.stylesheet; |
| 77 | + } |
| 78 | + this.stylesheet.Save(); |
| 79 | + } |
| 80 | +} |
0 commit comments