myesn

myEsn2E9

hi
github

NPOI: Export EXCEL(HSSF) Custom Cell Background Color

[!IMPORTANT]

  1. Different colors need to be set at different indexes, otherwise they will be overwritten.
  2. The range of indexes is limited, and custom colors need to be preset in the color palette, as explained in the comments.
var hssfworkbook = new HSSFWorkbook();
ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");

//fill background
ICellStyle style1 = hssfworkbook.CreateCellStyle();

#region Generate custom colors based on hex using the color palette

var palette = hssfworkbook.GetCustomPalette();
// hex to argb
// ColorTranslator static class:
// For .NET Framework, add a reference to the local DLL and then import the System.Drawing namespace
// For .NET Core, add the System.Drawing.Common package from NuGet and then import the System.Drawing namespace
var argb = ColorTranslator.FromHtml("#ff0303");
// The index value is index-8 when used in the underlying layer. The difference after subtraction must satisfy num >= 0 && num < 56
var customBackgroundColorIndexInPalette = (short)8;
palette.SetColorAtIndex(customBackgroundColorIndexInPalette, argb.R, argb.G, argb.B);

#endregion

// The background color of the cell must be set using the FillForegroundColor + FillPattern fields, and cannot be set using the FillBackgroundColor field
// Set the foreground color and fill pattern, with the foreground color taken from the RGB color at the specified index in the color palette
style1.FillForegroundColor = customBackgroundColorIndexInPalette; //NPOI.HSSF.Util.HSSFColor.Blue.Index;
style1.FillPattern = FillPattern.SolidForeground;

// Create a cell at position 0,0, set the cell content and style
var cell00 = sheet1.CreateRow(0).CreateCell(0);
cell00.SetCellValue("hi");
cell00.CellStyle = style1;

Custom RGB colors in the color palette:

// Custom color indexes (3 levels corresponding to 3 colors) in the Excel workbook's custom color palette
private static readonly short _color1IndexInWorkbookCustomPalette = 8;
private static readonly short _color2IndexInWorkbookCustomPalette = 9;
private static readonly short _color3IndexInWorkbookCustomPalette = 10;

// Preset all custom colors in the custom color palette of the Excel workbook
private static void SetAllColorIndexOnWorkbookCustomPalette(HSSFWorkbook workbook)
{
    SetColorIndexOnWorkbookCustomPalette(workbook, _color1IndexInWorkbookCustomPalette, ConfigHelper.Setting.Color1);
    SetColorIndexOnWorkbookCustomPalette(workbook, _color2IndexInWorkbookCustomPalette, ConfigHelper.Setting.Color2);
    SetColorIndexOnWorkbookCustomPalette(workbook, _color3IndexInWorkbookCustomPalette, ConfigHelper.Setting.Color3);
}
// Set the custom hex format color at the specified index in the custom color palette of the Excel workbook
private static void SetColorAtIndexOnWorkbookCustomPalette(HSSFWorkbook workbook,short rgbColorIndexInPalette, string hexColor)
{
    if (rgbColorIndexInPalette < 8 || rgbColorIndexInPalette > 56) throw new IndexOutOfRangeException(nameof(rgbColorIndexInPalette));

    var palette = workbook.GetCustomPalette();
    var argb = ColorTranslator.FromHtml(hexColor);
    palette.SetColorAtIndex(rgbColorIndexInPalette, argb.R, argb.G, argb.B);
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.