|
| 1 | +--- |
| 2 | +title: Set hex color in an Excel cell using XlsIO | Syncfusion |
| 3 | +description: Learn how to apply a hex color to an Excel cell with the Syncfusion .NET Excel library (XlsIO). Convert a hex string to RGB and assign it to CellStyle.Color. |
| 4 | +platform: document-processing |
| 5 | +control: XlsIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +How to set a hex color value for a cell? |
| 10 | + |
| 11 | +XlsIO does not provide a direct API to assign a hex color string to a cell style. Convert the hex value to an RGB Color and then assign it to CellStyle.Color. The following example demonstrates this approach. |
| 12 | + |
| 13 | +{% tabs %} |
| 14 | +{% highlight c# tabtitle="C# [Cross-platform]" %} |
| 15 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 16 | +{ |
| 17 | + IApplication application = excelEngine.Excel; |
| 18 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 19 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 20 | + |
| 21 | + // Set hex color |
| 22 | + worksheet["A1"].CellStyle.Color = HexToRgb("#FF0000"); |
| 23 | + |
| 24 | + workbook.SaveAs("HexColor.xlsx"); |
| 25 | + workbook.Close(); |
| 26 | + excelEngine.Dispose(); |
| 27 | +} |
| 28 | + |
| 29 | +public static Color HexToRgb(string hexColor) |
| 30 | +{ |
| 31 | + hexColor = hexColor.TrimStart('#'); |
| 32 | + int hexValue = int.Parse(hexColor, System.Globalization.NumberStyles.HexNumber); |
| 33 | + Color rgbColor = Color.FromArgb( |
| 34 | + hexValue >> 16) & 0xFF, // Red |
| 35 | + hexValue >> 8) & 0xFF, // Green |
| 36 | + hexValue & 0xFF // Blue |
| 37 | + ); |
| 38 | + |
| 39 | + return rgbColor; |
| 40 | +} |
| 41 | +{% endhighlight %} |
| 42 | + |
| 43 | +{% highlight c# tabtitle="C# [Windows-specific]" %} |
| 44 | +using (ExcelEngine excelEngine = new ExcelEngine()) |
| 45 | +{ |
| 46 | + IApplication application = excelEngine.Excel; |
| 47 | + IWorkbook workbook = application.Workbooks.Create(1); |
| 48 | + IWorksheet worksheet = workbook.Worksheets[0]; |
| 49 | + |
| 50 | + // Set hex color |
| 51 | + worksheet["A1"].CellStyle.Color = HexToRgb("#FF0000"); |
| 52 | + |
| 53 | + workbook.SaveAs("HexColor.xlsx"); |
| 54 | + workbook.Close(); |
| 55 | + excelEngine.Dispose(); |
| 56 | +} |
| 57 | + |
| 58 | +public static Color HexToRgb(string hexColor) |
| 59 | +{ |
| 60 | + hexColor = hexColor.TrimStart('#'); |
| 61 | + int hexValue = int.Parse(hexColor, System.Globalization.NumberStyles.HexNumber); |
| 62 | + Color rgbColor = Color.FromArgb( |
| 63 | + (hexValue >> 16) & 0xFF, // Red |
| 64 | + (hexValue >> 8) & 0xFF, // Green |
| 65 | + hexValue & 0xFF // Blue |
| 66 | + ); |
| 67 | + |
| 68 | + return rgbColor; |
| 69 | +} |
| 70 | +{% endhighlight %} |
| 71 | + |
| 72 | +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} |
| 73 | +Using excelEngine As New ExcelEngine() |
| 74 | + Dim application As IApplication = excelEngine.Excel |
| 75 | + Dim workbook As IWorkbook = application.Workbooks.Create(1) |
| 76 | + Dim worksheet As IWorksheet = workbook.Worksheets(0) |
| 77 | + |
| 78 | + ' Set hex color |
| 79 | + worksheet("A1").CellStyle.Color = HexToRgb("#FF0000") |
| 80 | + |
| 81 | + workbook.SaveAs("HexColor.xlsx") |
| 82 | + workbook.Close() |
| 83 | +End Using |
| 84 | + |
| 85 | +Public Function HexToRgb(hexColor As String) As Color |
| 86 | + hexColor = hexColor.TrimStart("#"c) |
| 87 | + Dim hexValue As Integer = Integer.Parse(hexColor, NumberStyles.HexNumber, CultureInfo.InvariantCulture) |
| 88 | + Dim rgbColor As Color = Color.FromArgb( |
| 89 | + (hexValue >> 16) And &HFF, ' Red |
| 90 | + (hexValue >> 8) And &HFF, ' Green |
| 91 | + hexValue And &HFF ' Blue |
| 92 | + ) |
| 93 | + Return rgbColor |
| 94 | +End Function |
| 95 | +{% endhighlight %} |
| 96 | +{% endtabs %} |
| 97 | + |
| 98 | +## See Also |
| 99 | + |
| 100 | +* [How to get the RGB color value for the applied cell color?](how-to-get-the-rgb-color-value-for-the-applied-cell-color) |
| 101 | +* [How to get RGB values of a cell’s background color?](how-to-get-rgb-values-of-a-cells-background-color) |
| 102 | +* [XlsIO support for cell background color transparency in Excel](does-xlsio-support-opacity-or-transparency-for-cell-background-colors-in-excel) |
0 commit comments