Skip to content

Commit 40b1fb7

Browse files
author
Fahad Adeel
committed
Comprehensive Revamp of the Workbook Class
- Overhauled the structure and functionality of the `Workbook` class for improved clarity and encapsulation: * Separated internal and public-facing members to provide a cleaner interface. * Initialized resources like `SpreadsheetDocument`, `WorkbookPart`, and `WorksheetPart` in constructors for consistent setup. - Enhanced workbook creation and reading functionalities: * Introduced default constructor to create a new workbook with essential parts, and another constructor for reading from a file. * Added validation for file existence and non-empty paths. - Incorporated worksheet management methods: * Added `AddSheet`, `RemoveSheet`, and `SyncWorksheets` to facilitate adding, removing, and syncing worksheets in the workbook. - Optimized style management: * Integrated a `StyleUtility` member to handle style-related tasks. * Added `UpdateDefaultStyle` and `CreateStyle` methods for better style handling. * Validated hex colors and other style parameters. - Improved document properties access: * Provided getters and setters for built-in document properties, allowing direct interaction with essential metadata. - Enhanced saving capabilities: * Implemented multiple save methods to support saving to a specified file path, a stream, or the original file path. * Ensured memory streams are written back to the files appropriately. - Implemented IDisposable pattern: * Properly released resources with the `Dispose` method to ensure efficient memory management. Overall, this revamp focuses on making the `Workbook` class more efficient, modular, and user-friendly.
1 parent d92958b commit 40b1fb7

File tree

4 files changed

+332
-247
lines changed

4 files changed

+332
-247
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
namespace FileFormat.Cells
3+
{
4+
public class BuiltInDocumentProperties
5+
{
6+
public string Author { get; set; }
7+
public string Title { get; set; }
8+
public DateTime CreatedDate { get; set; }
9+
public string ModifiedBy { get; set; }
10+
public DateTime ModifiedDate { get; set; }
11+
public string Subject { get; set; }
12+
13+
// ... other properties as needed ...
14+
}
15+
16+
}
17+

FileFormat.Cells/CellStyle.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

FileFormat.Cells/StyleUtility.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)