Skip to content

Commit d90474c

Browse files
committed
Initial Release v25.1.0
- Create, read, and manipulate Wordprocessing documents (.docx). - Create, read, and manipulate Excel spreadsheets (.xlsx). - Create, read, and manipulate PowerPoint presentations (.pptx).
1 parent bcbdd6e commit d90474c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+36255
-0
lines changed

Excel/BuiltInDocumentProperties.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
namespace Openize.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+

Excel/Cell.cs

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
using System;
2+
using DocumentFormat.OpenXml;
3+
using System.Globalization;
4+
using DocumentFormat.OpenXml.Spreadsheet;
5+
using DocumentFormat.OpenXml.Packaging;
6+
using System.Linq;
7+
8+
namespace Openize.Cells
9+
{
10+
public sealed class Cell
11+
{
12+
13+
private readonly DocumentFormat.OpenXml.Spreadsheet.Cell _cell;
14+
private readonly WorkbookPart _workbookPart;
15+
16+
private readonly SheetData _sheetData;
17+
18+
/// <summary>
19+
/// Gets the cell reference in A1 notation.
20+
/// </summary>
21+
public string CellReference => _cell.CellReference;
22+
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="Cell"/> class.
25+
/// </summary>
26+
/// <param name="cell">The underlying OpenXML cell object.</param>
27+
/// <param name="sheetData">The sheet data containing the cell.</param>
28+
/// <exception cref="ArgumentNullException">
29+
/// Thrown when <paramref name="cell"/> or <paramref name="sheetData"/> is null.
30+
/// </exception>
31+
public Cell(DocumentFormat.OpenXml.Spreadsheet.Cell cell, SheetData sheetData, WorkbookPart workbookPart)
32+
{
33+
_cell = cell ?? throw new ArgumentNullException(nameof(cell));
34+
_sheetData = sheetData ?? throw new ArgumentNullException(nameof(sheetData));
35+
_workbookPart = workbookPart ?? throw new ArgumentNullException(nameof(workbookPart));
36+
}
37+
38+
/// <summary>
39+
/// Adds a hyperlink to the specified cell with an optional tooltip.
40+
/// </summary>
41+
/// <param name="hyperlinkUrl">The URL of the hyperlink to be added.</param>
42+
/// <param name="tooltip">The optional tooltip to display when hovering over the hyperlink.</param>
43+
/// <exception cref="ArgumentException">
44+
/// Thrown when <paramref name="hyperlinkUrl"/> is null or empty.
45+
/// </exception>
46+
/// <exception cref="InvalidOperationException">
47+
/// Thrown when the parent WorksheetPart or the OpenXML Worksheet cannot be found.
48+
/// </exception>
49+
/// <remarks>
50+
/// This method creates a hyperlink relationship in the parent worksheet and adds a Hyperlink element
51+
/// to the cell's reference in the worksheet. The method does not modify the cell's text; use <see cref="PutValue"/>
52+
/// to set display text if needed.
53+
/// </remarks>
54+
/// <example>
55+
/// The following example demonstrates how to use the <c>SetHyperlink</c> method:
56+
/// <code>
57+
/// using (Workbook wb = new Workbook("path/to/your/file.xlsx"))
58+
/// {
59+
/// Worksheet sheet = wb.Worksheets[0];
60+
/// Cell cell = sheet.Cells["A1"];
61+
/// cell.PutValue("Click Me");
62+
/// cell.SetHyperlink("https://example.com", "Visit Example");
63+
/// wb.Save("path/to/your/file.xlsx");
64+
/// }
65+
/// </code>
66+
/// </example>
67+
public void SetHyperlink(string hyperlinkUrl, string tooltip = null)
68+
{
69+
if (string.IsNullOrEmpty(hyperlinkUrl))
70+
throw new ArgumentException("Hyperlink URL cannot be null or empty.", nameof(hyperlinkUrl));
71+
72+
// Get the WorksheetPart for this cell
73+
WorksheetPart worksheetPart = GetWorksheetPart();
74+
if (worksheetPart == null)
75+
throw new InvalidOperationException("WorksheetPart is not available for this cell.");
76+
77+
// Get the underlying OpenXML Worksheet
78+
var openXmlWorksheet = worksheetPart.Worksheet;
79+
if (openXmlWorksheet == null)
80+
throw new InvalidOperationException("The OpenXML Worksheet is not available.");
81+
82+
// Ensure the Hyperlinks collection exists
83+
Hyperlinks hyperlinks = openXmlWorksheet.GetFirstChild<Hyperlinks>();
84+
if (hyperlinks == null)
85+
{
86+
hyperlinks = new Hyperlinks();
87+
openXmlWorksheet.InsertAfter(hyperlinks, openXmlWorksheet.GetFirstChild<SheetData>());
88+
}
89+
90+
// Create a unique relationship ID
91+
string relationshipId = "rId" + Guid.NewGuid().ToString();
92+
93+
// Add the hyperlink relationship to the worksheet part
94+
worksheetPart.AddHyperlinkRelationship(new Uri(hyperlinkUrl, UriKind.Absolute), true, relationshipId);
95+
96+
// Create and add the Hyperlink element
97+
Hyperlink hyperlink = new Hyperlink
98+
{
99+
Reference = CellReference, // The reference of this cell in A1 notation
100+
Tooltip = tooltip,
101+
Id = relationshipId
102+
};
103+
hyperlinks.Append(hyperlink);
104+
105+
// Save changes to the worksheet
106+
openXmlWorksheet.Save();
107+
}
108+
109+
/// <summary>
110+
/// Retrieves the WorksheetPart containing this cell.
111+
/// </summary>
112+
/// <returns>The <see cref="WorksheetPart"/> that contains the current cell.</returns>
113+
/// <exception cref="InvalidOperationException">
114+
/// Thrown if the WorksheetPart cannot be located.
115+
/// </exception>
116+
/// <remarks>
117+
/// This method searches all WorksheetParts in the parent WorkbookPart and identifies
118+
/// the one containing the current SheetData.
119+
/// </remarks>
120+
private WorksheetPart GetWorksheetPart()
121+
{
122+
foreach (var worksheetPart in _workbookPart.GetPartsOfType<WorksheetPart>())
123+
{
124+
if (worksheetPart.Worksheet.Descendants<SheetData>().Contains(_sheetData))
125+
{
126+
return worksheetPart;
127+
}
128+
}
129+
130+
throw new InvalidOperationException("Unable to find the WorksheetPart containing this cell.");
131+
}
132+
133+
/// <summary>
134+
/// Sets the value of the cell as a string.
135+
/// </summary>
136+
/// <param name="value">The value to set.</param>
137+
public void PutValue(string value)
138+
{
139+
PutValue(value, CellValues.String);
140+
}
141+
142+
/// <summary>
143+
/// Sets the value of the cell as a number.
144+
/// </summary>
145+
/// <param name="value">The numeric value to set.</param>
146+
public void PutValue(double value)
147+
{
148+
PutValue(value.ToString(CultureInfo.InvariantCulture), CellValues.Number);
149+
}
150+
151+
/// <summary>
152+
/// Sets the value of the cell as a date.
153+
/// </summary>
154+
/// <param name="value">The date value to set.</param>
155+
public void PutValue(DateTime value)
156+
{
157+
PutValue(value.ToOADate().ToString(CultureInfo.InvariantCulture), CellValues.Date);
158+
}
159+
160+
/// <summary>
161+
/// Sets the cell's value with a specific data type.
162+
/// </summary>
163+
/// <param name="value">The value to set.</param>
164+
/// <param name="dataType">The data type of the value.</param>
165+
private void PutValue(string value, CellValues dataType)
166+
{
167+
_cell.DataType = new EnumValue<CellValues>(dataType);
168+
_cell.CellValue = new CellValue(value);
169+
170+
}
171+
172+
/// <summary>
173+
/// Sets a formula for the cell.
174+
/// </summary>
175+
/// <param name="formula">The formula to set.</param>
176+
public void PutFormula(string formula)
177+
{
178+
_cell.CellFormula = new CellFormula(formula);
179+
_cell.CellValue = new CellValue(); // You might want to set some default value or calculated value here
180+
}
181+
182+
/// <summary>
183+
/// Gets the value of the cell.
184+
/// </summary>
185+
/// <returns>The cell value as a string.</returns>
186+
public string GetValue()
187+
{
188+
if (_cell == null || _cell.CellValue == null) return "";
189+
190+
if (_cell.DataType != null && _cell.DataType.Value == CellValues.SharedString)
191+
{
192+
int index = int.Parse(_cell.CellValue.Text);
193+
SharedStringTablePart sharedStrings = _workbookPart.SharedStringTablePart;
194+
return sharedStrings.SharedStringTable.ElementAt(index).InnerText;
195+
}
196+
else
197+
{
198+
return _cell.CellValue.Text;
199+
}
200+
}
201+
202+
/// <summary>
203+
/// Gets the data type of the cell's value.
204+
/// </summary>
205+
/// <returns>The cell's value data type, or null if not set.</returns>
206+
public CellValues? GetDataType()
207+
{
208+
return _cell.DataType?.Value;
209+
}
210+
211+
212+
/// <summary>
213+
/// Gets the formula set for the cell.
214+
/// </summary>
215+
/// <returns>The cell's formula as a string, or null if not set.</returns>
216+
public string GetFormula()
217+
{
218+
return _cell.CellFormula?.Text;
219+
}
220+
221+
/// <summary>
222+
/// Applies a style to the cell.
223+
/// </summary>
224+
/// <param name="styleIndex">The index of the style to apply.</param>
225+
public void ApplyStyle(uint styleIndex)
226+
{
227+
_cell.StyleIndex = styleIndex;
228+
}
229+
}
230+
231+
}
232+

Excel/Comment.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Openize.Cells
8+
{
9+
public class Comment
10+
{
11+
public string Author { get; set; }
12+
public string Text { get; set; }
13+
14+
public Comment(string author, string text)
15+
{
16+
Author = author;
17+
Text = text;
18+
}
19+
}
20+
}

Excel/Image.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using DocumentFormat.OpenXml;
2+
using DocumentFormat.OpenXml.Packaging;
3+
4+
using A = DocumentFormat.OpenXml.Drawing;
5+
using Xdr = DocumentFormat.OpenXml.Drawing.Spreadsheet;
6+
using A14 = DocumentFormat.OpenXml.Office2010.Drawing;
7+
using DocumentFormat.OpenXml.Spreadsheet;
8+
using System.Linq;
9+
using System.IO;
10+
using System;
11+
12+
namespace Openize.Cells
13+
{
14+
15+
/// <summary>
16+
/// Represents an image, providing methods and properties to handle its path, data, and extension.
17+
/// </summary>
18+
public class Image
19+
{
20+
/// <summary>
21+
/// Gets the path of the image if initialized using a file path.
22+
/// </summary>
23+
public string Path { get; }
24+
25+
/// <summary>
26+
/// Gets the stream data of the image if initialized using a stream.
27+
/// </summary>
28+
public Stream Data { get; }
29+
30+
/// <summary>
31+
/// Gets the file extension of the image.
32+
/// </summary>
33+
public string Extension { get; }
34+
35+
36+
/// <summary>
37+
/// Initializes a new instance of the <see cref="Image"/> class using a file path.
38+
/// </summary>
39+
/// <param name="path">The path to the image file.</param>
40+
/// <exception cref="ArgumentException">
41+
/// Thrown when <paramref name="path"/> is null or empty or when the file does not exist.
42+
/// </exception>
43+
public Image(string path)
44+
{
45+
if (string.IsNullOrEmpty(path) || !File.Exists(path))
46+
throw new ArgumentException("Valid path required", nameof(path));
47+
48+
Path = path;
49+
Extension = System.IO.Path.GetExtension(path);
50+
}
51+
52+
/// <summary>
53+
/// Initializes a new instance of the <see cref="Image"/> class using a stream and a file extension.
54+
/// </summary>
55+
/// <param name="data">The stream containing the image data.</param>
56+
/// <param name="extension">The file extension of the image.</param>
57+
/// <exception cref="ArgumentNullException">
58+
/// Thrown when <paramref name="data"/> or <paramref name="extension"/> is null.
59+
/// </exception>
60+
public Image(Stream data, string extension)
61+
{
62+
Data = data ?? throw new ArgumentNullException(nameof(data));
63+
Extension = extension ?? throw new ArgumentNullException(nameof(extension));
64+
}
65+
66+
}
67+
68+
69+
70+
71+
}
72+

0 commit comments

Comments
 (0)