|
| 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 | + |
0 commit comments