1- using System ;
2- using DocumentFormat . OpenXml ;
3- using System . Globalization ;
4- using DocumentFormat . OpenXml . Spreadsheet ;
5- using DocumentFormat . OpenXml . Packaging ;
6-
7- namespace FileFormat . Cells
8- {
9- public sealed class Cell
10- {
11-
12- private readonly DocumentFormat . OpenXml . Spreadsheet . Cell _cell ;
1+ using System ;
2+ using DocumentFormat . OpenXml ;
3+ using System . Globalization ;
4+ using DocumentFormat . OpenXml . Spreadsheet ;
5+ using DocumentFormat . OpenXml . Packaging ;
6+
7+ namespace FileFormat . Cells
8+ {
9+ public sealed class Cell
10+ {
11+
12+ private readonly DocumentFormat . OpenXml . Spreadsheet . Cell _cell ;
1313 private readonly WorkbookPart _workbookPart ;
14-
15- private readonly SheetData _sheetData ;
16-
17- /// <summary>
18- /// Gets the cell reference in A1 notation.
19- /// </summary>
20- public string CellReference => _cell . CellReference ;
21-
22- /// <summary>
23- /// Initializes a new instance of the <see cref="Cell"/> class.
24- /// </summary>
25- /// <param name="cell">The underlying OpenXML cell object.</param>
26- /// <param name="sheetData">The sheet data containing the cell.</param>
27- /// <exception cref="ArgumentNullException">
28- /// Thrown when <paramref name="cell"/> or <paramref name="sheetData"/> is null.
29- /// </exception>
30- public Cell ( DocumentFormat . OpenXml . Spreadsheet . Cell cell , SheetData sheetData , WorkbookPart workbookPart )
31- {
32- _cell = cell ?? throw new ArgumentNullException ( nameof ( cell ) ) ;
33- _sheetData = sheetData ?? throw new ArgumentNullException ( nameof ( sheetData ) ) ;
34- _workbookPart = workbookPart ?? throw new ArgumentNullException ( nameof ( workbookPart ) ) ;
35- }
36-
37- /// <summary>
38- /// Sets the value of the cell as a string.
39- /// </summary>
40- /// <param name="value">The value to set.</param>
41- public void PutValue ( string value )
42- {
43- PutValue ( value , CellValues . String ) ;
44- }
45-
46- /// <summary>
47- /// Sets the value of the cell as a number.
48- /// </summary>
49- /// <param name="value">The numeric value to set.</param>
50- public void PutValue ( double value )
51- {
52- PutValue ( value . ToString ( CultureInfo . InvariantCulture ) , CellValues . Number ) ;
53- }
54-
55- /// <summary>
56- /// Sets the value of the cell as a date.
57- /// </summary>
58- /// <param name="value">The date value to set.</param>
59- public void PutValue ( DateTime value )
60- {
61- PutValue ( value . ToOADate ( ) . ToString ( CultureInfo . InvariantCulture ) , CellValues . Date ) ;
62- }
63-
64- /// <summary>
65- /// Sets the cell's value with a specific data type.
66- /// </summary>
67- /// <param name="value">The value to set.</param>
68- /// <param name="dataType">The data type of the value.</param>
69- private void PutValue ( string value , CellValues dataType )
70- {
71- _cell . DataType = new EnumValue < CellValues > ( dataType ) ;
72- _cell . CellValue = new CellValue ( value ) ;
73-
74- }
75-
76- /// <summary>
77- /// Sets a formula for the cell.
78- /// </summary>
79- /// <param name="formula">The formula to set.</param>
80- public void PutFormula ( string formula )
81- {
82- _cell . CellFormula = new CellFormula ( formula ) ;
83- _cell . CellValue = new CellValue ( ) ; // You might want to set some default value or calculated value here
84- }
85-
86- /// <summary>
87- /// Gets the value of the cell.
88- /// </summary>
89- /// <returns>The cell value as a string.</returns>
14+
15+ private readonly SheetData _sheetData ;
16+
17+ /// <summary>
18+ /// Gets the cell reference in A1 notation.
19+ /// </summary>
20+ public string CellReference => _cell . CellReference ;
21+
22+ /// <summary>
23+ /// Initializes a new instance of the <see cref="Cell"/> class.
24+ /// </summary>
25+ /// <param name="cell">The underlying OpenXML cell object.</param>
26+ /// <param name="sheetData">The sheet data containing the cell.</param>
27+ /// <exception cref="ArgumentNullException">
28+ /// Thrown when <paramref name="cell"/> or <paramref name="sheetData"/> is null.
29+ /// </exception>
30+ public Cell ( DocumentFormat . OpenXml . Spreadsheet . Cell cell , SheetData sheetData , WorkbookPart workbookPart )
31+ {
32+ _cell = cell ?? throw new ArgumentNullException ( nameof ( cell ) ) ;
33+ _sheetData = sheetData ?? throw new ArgumentNullException ( nameof ( sheetData ) ) ;
34+ _workbookPart = workbookPart ?? throw new ArgumentNullException ( nameof ( workbookPart ) ) ;
35+ }
36+
37+ /// <summary>
38+ /// Sets the value of the cell as a string.
39+ /// </summary>
40+ /// <param name="value">The value to set.</param>
41+ public void PutValue ( string value )
42+ {
43+ PutValue ( value , CellValues . String ) ;
44+ }
45+
46+ /// <summary>
47+ /// Sets the value of the cell as a number.
48+ /// </summary>
49+ /// <param name="value">The numeric value to set.</param>
50+ public void PutValue ( double value )
51+ {
52+ PutValue ( value . ToString ( CultureInfo . InvariantCulture ) , CellValues . Number ) ;
53+ }
54+
55+ /// <summary>
56+ /// Sets the value of the cell as a date.
57+ /// </summary>
58+ /// <param name="value">The date value to set.</param>
59+ public void PutValue ( DateTime value )
60+ {
61+ PutValue ( value . ToOADate ( ) . ToString ( CultureInfo . InvariantCulture ) , CellValues . Date ) ;
62+ }
63+
64+ /// <summary>
65+ /// Sets the cell's value with a specific data type.
66+ /// </summary>
67+ /// <param name="value">The value to set.</param>
68+ /// <param name="dataType">The data type of the value.</param>
69+ private void PutValue ( string value , CellValues dataType )
70+ {
71+ _cell . DataType = new EnumValue < CellValues > ( dataType ) ;
72+ _cell . CellValue = new CellValue ( value ) ;
73+
74+ }
75+
76+ /// <summary>
77+ /// Sets a formula for the cell.
78+ /// </summary>
79+ /// <param name="formula">The formula to set.</param>
80+ public void PutFormula ( string formula )
81+ {
82+ _cell . CellFormula = new CellFormula ( formula ) ;
83+ _cell . CellValue = new CellValue ( ) ; // You might want to set some default value or calculated value here
84+ }
85+
86+ /// <summary>
87+ /// Gets the value of the cell.
88+ /// </summary>
89+ /// <returns>The cell value as a string.</returns>
9090 public string GetValue ( )
9191 {
9292 if ( _cell == null || _cell . CellValue == null ) return "" ;
@@ -101,36 +101,36 @@ public string GetValue()
101101 {
102102 return _cell . CellValue . Text ;
103103 }
104- }
105-
106- /// <summary>
107- /// Gets the data type of the cell's value.
108- /// </summary>
109- /// <returns>The cell's value data type, or null if not set.</returns>
110- public CellValues ? GetDataType ( )
111- {
112- return _cell . DataType ? . Value ;
113- }
114-
115-
116- /// <summary>
117- /// Gets the formula set for the cell.
118- /// </summary>
119- /// <returns>The cell's formula as a string, or null if not set.</returns>
120- public string GetFormula ( )
121- {
122- return _cell . CellFormula ? . Text ;
123- }
124-
125- /// <summary>
126- /// Applies a style to the cell.
127- /// </summary>
128- /// <param name="styleIndex">The index of the style to apply.</param>
129- public void ApplyStyle ( uint styleIndex )
130- {
131- _cell . StyleIndex = styleIndex ;
132- }
133- }
134-
135- }
136-
104+ }
105+
106+ /// <summary>
107+ /// Gets the data type of the cell's value.
108+ /// </summary>
109+ /// <returns>The cell's value data type, or null if not set.</returns>
110+ public CellValues ? GetDataType ( )
111+ {
112+ return _cell . DataType ? . Value ;
113+ }
114+
115+
116+ /// <summary>
117+ /// Gets the formula set for the cell.
118+ /// </summary>
119+ /// <returns>The cell's formula as a string, or null if not set.</returns>
120+ public string GetFormula ( )
121+ {
122+ return _cell . CellFormula ? . Text ;
123+ }
124+
125+ /// <summary>
126+ /// Applies a style to the cell.
127+ /// </summary>
128+ /// <param name="styleIndex">The index of the style to apply.</param>
129+ public void ApplyStyle ( uint styleIndex )
130+ {
131+ _cell . StyleIndex = styleIndex ;
132+ }
133+ }
134+
135+ }
136+
0 commit comments