NanoXLSX.Formatting 3.0.0
Loading...
Searching...
No Matches
WorksheetExtensions.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System.Collections.Generic;
9using System.ComponentModel;
10using NanoXLSX.Exceptions;
11using NanoXLSX.Styles;
12
13namespace NanoXLSX
14{
18 [EditorBrowsable(EditorBrowsableState.Never)]
19 public static class WorksheetExtensions
20 {
30 public static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, int columnNumber, int rowNumber)
31 {
32 AddFormattedText(worksheet, formattedText, columnNumber, rowNumber);
33 }
34
45
46 public static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, int columnNumber, int rowNumber, Style style)
47 {
48 AddFormattedText(worksheet, formattedText, columnNumber, rowNumber, style);
49 }
50
60 public static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, string address)
61 {
62 int column;
63 int row;
64 Cell.ResolveCellCoordinate(address, out column, out row);
65 AddFormattedText(worksheet, formattedText, column, row);
66 }
67
78 public static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, string address, Style style)
79 {
80 int column;
81 int row;
82 Cell.ResolveCellCoordinate(address, out column, out row);
83 AddFormattedText(worksheet, formattedText, column, row, style);
84 }
85
94 public static void AddNextFormattedTextCell(this Worksheet worksheet, FormattedText formattedText)
95 {
96 AddFormattedText(worksheet, formattedText);
97 }
98
108 public static void AddNextFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, Style style)
109 {
110 AddFormattedText(worksheet, formattedText, style);
111 }
112
124 public static void AddFormattedTextCellRange(this Worksheet worksheet, IReadOnlyList<FormattedText> values, string startAddress, string endAddress, Style style = null)
125 {
126 AddFormattedTextRange(worksheet, values, (Address)startAddress, (Address)endAddress, style);
127 }
128
140 public static void AddFormattedTextCellRange(this Worksheet worksheet, IReadOnlyList<FormattedText> values, Address startAddress, Address endAddress, Style style = null)
141 {
142 AddFormattedTextRange(worksheet, values, startAddress, endAddress, style);
143 }
144
156 public static void AddFormattedTextCellRange(this Worksheet worksheet, IReadOnlyList<FormattedText> values, string cellRange, Style style = null)
157 {
158 Range range = new Range(cellRange);
159 AddFormattedTextRange(worksheet, values, range.StartAddress, range.EndAddress, style);
160 }
161
162
172 private static void AddFormattedText(Worksheet worksheet, FormattedText formattedText, int columnNumber, int rowNumber, Style cellStyle = null)
173 {
174 if (formattedText == null)
175 {
176 throw new WorksheetException("A formatted text to add cannot be null");
177 }
178 if (formattedText.WrapText)
179 {
180 if (cellStyle == null)
181 {
182 worksheet.AddCell(formattedText, columnNumber, rowNumber, FormattedText.LineBreakStyle);
183 }
184 else
185 {
186 Style mergedStyle = cellStyle.Append(FormattedText.LineBreakStyle);
187 worksheet.AddCell(formattedText, columnNumber, rowNumber, mergedStyle);
188 }
189 }
190 else
191 {
192 worksheet.AddCell(formattedText, columnNumber, rowNumber, cellStyle);
193 }
194 }
195
203 private static void AddFormattedText(Worksheet worksheet, FormattedText formattedText, Style cellStyle = null)
204 {
205 if (formattedText == null)
206 {
207 throw new WorksheetException("A formatted text to add cannot be null");
208 }
209 if (formattedText.WrapText)
210 {
211 if (cellStyle == null)
212 {
213 worksheet.AddNextCell(formattedText, FormattedText.LineBreakStyle);
214 }
215 else
216 {
217 Style mergedStyle = cellStyle.Append(FormattedText.LineBreakStyle);
218 worksheet.AddNextCell(formattedText, mergedStyle);
219 }
220 }
221 else
222 {
223 worksheet.AddNextCell(formattedText, cellStyle);
224 }
225 }
226
236 private static void AddFormattedTextRange(Worksheet worksheet, IReadOnlyList<FormattedText> formattedTexts, Address startAddress, Address endAddress, Style cellStyle = null)
237 {
238 if (formattedTexts == null || formattedTexts.Count == 0)
239 {
240 throw new WorksheetException("A range of formatted texts to add cannot be null or empty");
241 }
242 List<Cell> cells = new List<Cell>(formattedTexts.Count);
243 foreach (FormattedText text in formattedTexts)
244 {
245 if (text == null || string.IsNullOrEmpty(text.PlainText))
246 {
247 Cell emptyCell = new Cell(null, Cell.CellType.Empty);
248 if (cellStyle != null)
249 {
250 emptyCell.SetStyle(cellStyle);
251 }
252 cells.Add(emptyCell);
253 continue;
254 }
255 Cell cell = new Cell(text, Cell.CellType.String);
256 if (text.WrapText && cellStyle != null)
257 {
258 Style merged = cellStyle.Append(FormattedText.LineBreakStyle);
259 cell.SetStyle(merged);
260 }
261 else if (text.WrapText && cellStyle == null)
262 {
263 cell.SetStyle(FormattedText.LineBreakStyle);
264 }
265 else if (!text.WrapText && cellStyle != null)
266 {
267 cell.SetStyle(cellStyle);
268 }
269 cells.Add(cell);
270 }
271 if (cellStyle != null)
272 {
273 worksheet.AddCellRange(cells, startAddress, endAddress, cellStyle);
274 }
275 else
276 {
277 worksheet.AddCellRange(cells, startAddress, endAddress);
278 }
279 }
280
281 }
282}
Represents a formatted text entry in Excel shared strings, supporting rich text with multiple runs an...
bool WrapText
Gets or sets whether the runs should be rendered with text wrapping, if there are line breaks present...
Writer extension methods for the Worksheet class.
static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, int columnNumber, int rowNumber, Style style)
Adds a formatted text to the specified cell with a style. If the WrapText property of the formatted t...
static void AddFormattedTextCellRange(this Worksheet worksheet, IReadOnlyList< FormattedText > values, string cellRange, Style style=null)
Adds a list of formatted texts to a defined cell range with a style applied to all cells....
static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, string address, Style style)
Adds a formatted text to the specified cell address with a style. If the WrapText property of the for...
static void AddFormattedTextCellRange(this Worksheet worksheet, IReadOnlyList< FormattedText > values, string startAddress, string endAddress, Style style=null)
Adds a list of formatted texts to a defined cell range. Values are distributed column-by-column from ...
static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, string address)
Adds a formatted text to the specified cell address. If the WrapText property of the formatted text i...
static void AddFormattedTextCellRange(this Worksheet worksheet, IReadOnlyList< FormattedText > values, Address startAddress, Address endAddress, Style style=null)
Adds a list of formatted texts to a defined cell range with a style applied to all cells....
static void AddFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, int columnNumber, int rowNumber)
Adds a formatted text to the specified cell. If the WrapText property of the formatted text is set to...
static void AddNextFormattedTextCell(this Worksheet worksheet, FormattedText formattedText, Style style)
Adds a formatted text to the next cell position with a style. The direction of the next cell depends ...
static void AddNextFormattedTextCell(this Worksheet worksheet, FormattedText formattedText)
Adds a formatted text to the next cell position. The direction of the next cell depends on the curren...