我们刚开始使用NPOI组件。
我们遇到了设置ICellStyle属性的FillForegroundColor的问题。
ICellStyle HeaderCellStyle = xssfworkbook.CreateCellStyle();
HeaderCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index;
FillForegroundColor期望类型为short。
我们如何设置不同的颜色而不是在HSSFColor中使用颜色。
我们需要设置为"RGB192:0:0"
,我们如何为ICellStyle属性FillForegroundColor做到这一点
有人可以通过一些例子帮助我们吗?
我找到了解决方案。请参考以下代码
byte[] rgb = new byte[3] { 192, 0, 0 };
XSSFCellStyle HeaderCellStyle1 = (XSSFCellStyle)xssfworkbook.CreateCellStyle();
HeaderCellStyle1.SetFillForegroundColor(new XSSFColor(rgb));
对不起,又来了。
Color SuperColor = Color.FromArgb(192, 0, 0);
styleH.FillForegroundColor = GetXLColour(hssfworkbook, SuperColor);
private short GetXLColour(HSSFWorkbook workbook, System.Drawing.Color SystemColour)
{
short s = 0;
HSSFPalette XlPalette = workbook.GetCustomPalette();
NPOI.HSSF.Util.HSSFColor XlColour = XlPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B);
if (XlColour == null)
{
if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 255)
{
if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 64)
{
NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE = 64;
NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE += 1;
XlColour = XlPalette.AddColor(SystemColour.R, SystemColour.G, SystemColour.B);
}
else
{
XlColour = XlPalette.FindSimilarColor(SystemColour.R, SystemColour.G, SystemColour.B);
}
s = XlColour.GetIndex();
}
}
else
s = XlColour.GetIndex();
return s;
}
对于2016年来到这里的人们,您可以在这里找到许多有用的颜色:
nameStyle.Color = NPOI.HSSF.Util.HSSFColor.BlueGrey.Index;
因此,BlueGrey是您正在寻找的颜色。
这是一个VB等效的答案,我不得不转换它,所以如果他们找到它可能会省一些工作:
'my workbook is from a template that already contains some header data
Dim fs As New FileStream(HttpContext.Current.Server.MapPath("..\_ExcelTemplates\filename.xlsx"), FileMode.Open, FileAccess.Read)
Dim workbook As IWorkbook
workbook = New XSSFWorkbook(fs)
Dim sheet_stats As ISheet = workbook.GetSheet("Stats")
'style
Dim rgb_grandTotalRow_fg() As Byte = New Byte() {197, 217, 241} 'lightish blue
Dim style_grandTotalRow As XSSFCellStyle = workbook.CreateCellStyle()
style_grandTotalRow.SetFillForegroundColor(New XSSFColor(rgb_grandTotalRow_fg)) 'XSSFCellStyle only for other use (FillForegroundColor = IndexedColors.LightTurquoise.Index)
style_grandTotalRow.FillPattern = FillPattern.SolidForeground
'apply the style to a cell
Dim rowHdr As IRow = sheet_stats.GetRow(2)
Dim cell As ICell
cell = Row.CreateCell(1) 'create cell at col index 1
cell.CellStyle = style_grandTotalRow
cell.SetCellValue("TEST")
改变字体颜色
例..
ICellStyle style0 = hssfworkbook.CreateCellStyle();
IFont font0 = hssfworkbook.CreateFont();
font0.Color = NPOI.HSSF.Util.HSSFColor.RED.index;
style0.SetFont(font0);
for (int i = 0; i < cell.Length; i++)
{
cell[i].CellStyle = style0;
}
--
我是日本人,不能英语。抱歉。