|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: Formatting in Blazor Spreadsheet Component | Syncfusion |
| 4 | +description: Checkout and learn all about formatting options in the Syncfusion Blazor Spreadsheet component | Syncfusion. |
| 5 | +platform: document-processing |
| 6 | +control: Spreadsheet |
| 7 | +documentation: ug |
| 8 | +--- |
| 9 | + |
| 10 | +# Formatting in Blazor Spreadsheet Component |
| 11 | + |
| 12 | +Formatting options improve data readability and presentation. The Blazor Spreadsheet component provides various formatting options, which can be categorized into: |
| 13 | + |
| 14 | +* Number Formatting |
| 15 | +* Text Formatting |
| 16 | +* Cell Formatting |
| 17 | + |
| 18 | +The entire formatting functionality can be globally enabled or disabled using the [`AllowCellFormatting`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_AllowCellFormatting) property. By default, `AllowCellFormatting` is set to **true**. |
| 19 | + |
| 20 | +## Number Formatting |
| 21 | + |
| 22 | +Number formatting in the Blazor Spreadsheet component controls how numeric, date, and time values are displayed without altering their underlying data. The component offers Excel-like number formats that are culture-aware, integrate with undo/redo operations, and respect worksheet protection settings. These formats can be applied through the Ribbon toolbar or programmatically. |
| 23 | + |
| 24 | +The number formatting functionality can be globally enabled or disabled using the [`AllowNumberFormatting`](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_AllowNumberFormatting) property. By default, `AllowNumberFormatting` is set to **true**. |
| 25 | + |
| 26 | +### Supported Categories |
| 27 | + |
| 28 | +The Blazor Spreadsheet component provides several built-in number format categories to control how numeric, date, and time values are displayed. These formats are culture-aware and ensure proper rendering of decimal separators, currency symbols, and date/time patterns. |
| 29 | + |
| 30 | +| Category | Example Format String | Result Example | |
| 31 | +|---|---|---| |
| 32 | +| General | `General` | 1234.567 | |
| 33 | +| Number | `#,##0.00` | 1234.57 | |
| 34 | +| Currency | `$#,##0.00` | $1,234.57 | |
| 35 | +| Accounting | `_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)` | $ 1,234.57 | |
| 36 | +| Percentage | `0.00%` | 8.90% | |
| 37 | +| Scientific | `0.00E+00` | 1.23E+03 | |
| 38 | +| Fraction | `# ?/?` | 123 1/2 | |
| 39 | +| Short Date | `m/d/yyyy` | 03/15/2025 | |
| 40 | +| Long Date | `dddd, mmmm dd, yyyy` | Saturday, March 15, 2025 | |
| 41 | +| Time | `h:mm:ss AM/PM` | 3:45:30 PM | |
| 42 | + |
| 43 | +### Applying Number Formats via the UI |
| 44 | + |
| 45 | +Number formats can be applied through the UI using the following method: |
| 46 | + |
| 47 | +* **Ribbon**: Navigate to the **Home** tab and use the **Number Format** dropdown. This dropdown displays previews of built-in formats based on the current culture. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +### Applying Number Formats Programmatically |
| 52 | + |
| 53 | +Number formats can be applied programmatically to the current selection or a specified range using the [NumberFormatAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_NumberFormatAsync_System_String_System_String_) method. This method accepts a format string and an optional cell address. |
| 54 | + |
| 55 | +| Parameter | Type | Description | |
| 56 | +| -- | -- | -- | |
| 57 | +| format | string | The built-in format or a supported custom pattern. | |
| 58 | +| cellAddress | string (Optional) | The address of the target range where the number format was applied (e.g., `"Sheet1!A2:A5"` or `"A2:A5"`).When cellAddress is omitted, the current selection is formatted. | |
| 59 | + |
| 60 | +{% tabs %} |
| 61 | +{% highlight razor tabtitle="Index.razor" %} |
| 62 | + |
| 63 | +@using Syncfusion.Blazor.Spreadsheet |
| 64 | + |
| 65 | +<button @onclick="ApplyFormat">Apply number format</button> |
| 66 | +<SfSpreadsheet @ref="SpreadsheetInstance" DataSource="DataSourceBytes"> |
| 67 | + <SpreadsheetRibbon></SpreadsheetRibbon> |
| 68 | +</SfSpreadsheet> |
| 69 | + |
| 70 | +@code { |
| 71 | + public SfSpreadsheet SpreadsheetInstance; |
| 72 | + public byte[] DataSourceBytes { get; set; } |
| 73 | + |
| 74 | + protected override void OnInitialized() |
| 75 | + { |
| 76 | + string filePath = "wwwroot/Sample.xlsx"; |
| 77 | + DataSourceBytes = File.ReadAllBytes(filePath); |
| 78 | + } |
| 79 | + |
| 80 | + private async Task ApplyFormat() |
| 81 | + { |
| 82 | + // Apply custom percentage format to range A2:A5 |
| 83 | + await SpreadsheetInstance.NumberFormatAsync("0.00%", "A2:A5"); |
| 84 | + // Apply custom short date format to cell D1 on Sheet2 |
| 85 | + await SpreadsheetInstance.NumberFormatAsync("mm/dd/yyyy", "Sheet2!D1"); |
| 86 | + } |
| 87 | +} |
| 88 | +{% endhighlight %} |
| 89 | +{% endtabs %} |
| 90 | + |
| 91 | +If the built-in formats do not meet specific requirements, custom patterns can be applied programmatically using the `NumberFormatAsync` method. Currently, a dedicated UI dialog for defining custom formats is not available. They are exclusively supported through the API. Patterns must be compatible with Excel-style format strings. |
| 92 | + |
| 93 | +## Text and Cell Formatting |
| 94 | + |
| 95 | +Text and cell formatting enhances the visual presentation of data by applying styles such as font changes, colors, borders, and alignment to individual cells or cell ranges. This helps organize content and emphasize important information for faster interpretation. |
| 96 | + |
| 97 | +### Text and cell formatting options include: |
| 98 | + |
| 99 | +* **Bold** - Applies a heavier font weight to make the text stand out in the Spreadsheet. |
| 100 | + |
| 101 | +* **Italic** - Slants the text to give it a distinct look, often used for emphasis or to highlight differences. |
| 102 | + |
| 103 | +* **Underline** - Adds a line below the text, commonly used for emphasis or to indicate hyperlinks. |
| 104 | + |
| 105 | +* **Strikethrough** - Draws a line through the text, often used to show completed tasks or outdated information. |
| 106 | + |
| 107 | +* **Font Family** - Changes the typeface of the text (e.g., Arial, Calibri, Times New Roman, and more) to enhance readability or visual appeal. |
| 108 | + |
| 109 | +* **Font Size** - Adjusts the size of the text to create visual hierarchy or improve readability in the Spreadsheet. |
| 110 | + |
| 111 | +* **Font Color** - Changes the color of the text to improve visual hierarchy or to organize information using color codes. |
| 112 | + |
| 113 | +* **Fill Color** - Adds color to the cell background to visually organize data or highlight important information. |
| 114 | + |
| 115 | +* **Horizontal Alignment** - Controls the position of text from left to right within a cell. Options include: |
| 116 | + * **Left** - Default for text |
| 117 | + * **Center** - Useful for headings |
| 118 | + * **Right** - Default for numbers |
| 119 | + |
| 120 | +* **Vertical Alignment** - Controls the position of text from top to bottom within a cell. Options include: |
| 121 | + * **Top** – Aligns content to the top of the cell |
| 122 | + * **Middle** – Centers content vertically |
| 123 | + * **Bottom** – Default alignment |
| 124 | + |
| 125 | +* **Wrap Text** - Displays long content on multiple lines within a single cell, preventing it from overflowing into adjacent cells. To enable text wrapping: |
| 126 | + 1. Select the target cell or range (e.g., C5). |
| 127 | + 2. Go to the Home tab. |
| 128 | + 3. Click Wrap Text in the ribbon to toggle text wrapping for the selected cells. |
| 129 | + |
| 130 | +Text and cell formatting can be applied or removed from a cell or range by using the options available in the component's built-in **Ribbon** under the **Home** tab. |
| 131 | + |
| 132 | +### Borders |
| 133 | + |
| 134 | +Borders visually separate cells and define tables or sections within a worksheet. The Blazor Spreadsheet component supports various border types, styles, colors, and sizes. The available border options are: |
| 135 | + |
| 136 | +| Border Type | Description | |
| 137 | +|-------------|-------------| |
| 138 | +| Top Border | Applies a border to the top edge of a cell or range of cells. | |
| 139 | +| Left Border | Applies a border to the left edge of a cell or range of cells. | |
| 140 | +| Right Border | Applies a border to the right edge of a cell or range of cells. | |
| 141 | +| Bottom Border | Applies a border to the bottom edge of a cell or range of cells. | |
| 142 | +| No Border | Removes all borders from a cell or range of cells. | |
| 143 | +| All Border | Applies borders to all sides of a cell or range of cells. | |
| 144 | +| Horizontal Border | Applies borders to the top and bottom edges of a cell or range. | |
| 145 | +| Vertical Border | Applies borders to the left and right edges of a cell or range. | |
| 146 | +| Outside Border | Applies borders to the outer edges of a range of cells. | |
| 147 | +| Inside Border | Applies borders to the inner edges of a range of cells | |
| 148 | + |
| 149 | +Border color, size, and style can also be customized. The supported sizes and styles are: |
| 150 | + |
| 151 | +| Type | Description | |
| 152 | +|--------|----------------------------------| |
| 153 | +| Thin | Specifies a `1px` border size (default). | |
| 154 | +| Medium | Specifies a `2px` border size. | |
| 155 | +| Thick | Specifies a `3px` border size. | |
| 156 | +| Solid | Creates a `solid` border (default). | |
| 157 | +| Dashed | Creates a `dashed` border.| |
| 158 | +| Dotted | Creates a `dotted` border.| |
| 159 | +| Double | Creates a `double` border.| |
| 160 | + |
| 161 | +### Applying Borders via the UI |
| 162 | + |
| 163 | +Borders can be applied through the UI using the following method: |
| 164 | + |
| 165 | +* **Ribbon**: Navigate to the **Home** tab in the Ribbon toolbar and select the **Borders** dropdown. Styling options such as color, size, and style are available within the same menu. |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +### Applying Borders Programmatically |
| 170 | + |
| 171 | +Borders can be applied programmatically to a specific cell or range of cells using the [SetBordersAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Spreadsheet.SfSpreadsheet.html#Syncfusion_Blazor_Spreadsheet_SfSpreadsheet_SetBordersAsync_Syncfusion_Blazor_Spreadsheet_BorderType_Syncfusion_XlsIO_ExcelLineStyle_System_String_System_String) method. The available parameters in the `SetBordersAsync` method are: |
| 172 | + |
| 173 | +| Parameter | Type | Description | |
| 174 | +| -- | -- | -- | |
| 175 | +| borderType | BorderType | Specifies the type of border to apply (e.g., `BorderType.OutsideBorders`, `BorderType.AllBorders`, `BorderType.TopBorder`). | |
| 176 | +| lineStyle | Syncfusion.XlsIO.ExcelLineStyle | Defines the line style of the border (e.g., `Syncfusion.XlsIO.ExcelLineStyle.Thin`, `Syncfusion.XlsIO.ExcelLineStyle.Medium`, `Syncfusion.XlsIO.ExcelLineStyle.Dashed`). | |
| 177 | +| borderColor | string | The border color in hexadecimal or named CSS color format (e.g., `"#000000"`, `"red"`, `"#2196F3"`). | |
| 178 | +| cellAddress | string (Optional) | The address of the target cell or range (e.g., `"A1"`, `"A1:C5"`, or `"Sheet2!B2:D4"`). If omitted, the operation targets the current selection. | |
| 179 | + |
| 180 | +{% tabs %} |
| 181 | +{% highlight razor tabtitle="Index.razor" %} |
| 182 | +@using Syncfusion.Blazor.Spreadsheet |
| 183 | +@using Syncfusion.XlsIO |
| 184 | + |
| 185 | +<button @onclick="ApplyBorders">Apply Borders</button> |
| 186 | +<SfSpreadsheet @ref="SpreadsheetInstance" DataSource="DataSourceBytes"></SfSpreadsheet> |
| 187 | + |
| 188 | +@code { |
| 189 | + public SfSpreadsheet SpreadsheetInstance { get; set; } |
| 190 | + public byte[] DataSourceBytes { get; set; } |
| 191 | + |
| 192 | + protected override void OnInitialized() |
| 193 | + { |
| 194 | + string filePath = "wwwroot/Sample.xlsx"; |
| 195 | + DataSourceBytes = File.ReadAllBytes(filePath); |
| 196 | + } |
| 197 | + public async Task ApplyBorders() |
| 198 | + { |
| 199 | + await SpreadsheetInstance.SetBordersAsync(BorderType.OutsideBorders, ExcelLineStyle.Medium, "#FF0000", "A1:C5"); |
| 200 | + await SpreadsheetInstance.SetBordersAsync(BorderType.AllBorders, ExcelLineStyle.Dashed, "#0000FF", "B2:D4"); |
| 201 | + } |
| 202 | +} |
| 203 | +{% endhighlight %} |
| 204 | +{% endtabs %} |
| 205 | + |
| 206 | +### Limitations |
| 207 | + |
| 208 | +* Conditional formatting is currently not supported in the Blazor Spreadsheet component. |
| 209 | +* A custom number format UI dialog is not available, custom formats must be applied using the API. |
0 commit comments