NPOI 自定义设置单元格背景颜色[RGB格式]
npoi自带的颜色有时不能满足我们要求时,我们需要自己定义背景色,而且npoi的颜色类型是short类型,而.net颜色类是color类型,怎么让它们相互之间转换呢?网上有一段代码是vb的可以解决上述问题,本人把它翻译成c#的,方便大家使用
vb:
private function getxlcolour(byval systemcolour as system.drawing.color) as short
'lookup rgb from .net system colour in excel pallete - or create a new entry (get nearest if palette full). return the xl palette index.
dim xlpalette as hssfpalette = xlworkbook.getcustompalette()
dim xlcolour as npoi.hssf.util.hssfcolor = xlpalette.findcolor(systemcolour.r, systemcolour.g, systemcolour.b)
if isnothing(xlcolour) then
'available colour palette entries: 65 to 32766 (0-64=standard palette; 64=auto, 32767=unspecified)
if npoi.hssf.record.paletterecord.standard_palette_size < 255 then
if npoi.hssf.record.paletterecord.standard_palette_size < 64 then 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)
end if
return xlcolour.getindex()
else
return xlcolour.getindex()
end if
end function
c#:
private short getxlcolour(hssfworkbook workbook, system.drawing.color systemcolour)
{
short s = 0;
hssfpalette xlpalette = workbook.getcustompalette();
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;
}
使用方法:
color levelonecolor = color.fromargb(143, 176, 229);
color leveltwocolor = color.fromargb(201, 217, 243);
color levelthreecolor = color.fromargb(231, 238, 248);
color levelfourcolor = color.fromargb(232, 230, 231);
color levelfivecolor = color.fromargb(250, 252, 213);
/// <summary>
/// 分层设置单元格样式
/// </summary>
/// <param name="workbook"></param>
/// <param name="alignment"></param>
/// <param name="valingment"></param>
/// <returns></returns>
public hssfcellstyle setstyle(hssfworkbook workbook, short alignment, short valingment, int layer)
{
hssfcellstyle style = workbook.createcellstyle();
style.alignment = alignment;
style.verticalalignment = valingment;
style.borderbottom = hssfcellstyle.border_thin;
style.borderleft = hssfcellstyle.border_thin;
style.borderright = hssfcellstyle.border_thin;
style.bordertop = hssfcellstyle.border_thin;
switch (layer)
{
case 0:
style.fillforegroundcolor = getxlcolour(workbook, levelonecolor); //调用getxlcolour方法
style.fillpattern = hssfcellstyle.alt_bars;
style.fillbackgroundcolor = getxlcolour(workbook, levelonecolor);
break;
case 1:
style.fillforegroundcolor = getxlcolour(workbook, leveltwocolor);
style.fillpattern = hssfcellstyle.alt_bars;
style.fillbackgroundcolor = getxlcolour(workbook, leveltwocolor);
break;
case 2:
style.fillforegroundcolor = getxlcolour(workbook, levelthreecolor);
style.fillpattern = hssfcellstyle.alt_bars;
style.fillbackgroundcolor = getxlcolour(workbook, levelthreecolor);
break;
case 3:
style.fillforegroundcolor = getxlcolour(workbook, levelfourcolor);
style.fillpattern = hssfcellstyle.alt_bars;
style.fillbackgroundcolor = getxlcolour(workbook, levelfourcolor);
break;
case 4:
style.fillforegroundcolor = getxlcolour(workbook, levelfivecolor);
style.fillpattern = hssfcellstyle.alt_bars;
style.fillbackgroundcolor = getxlcolour(workbook, levelfivecolor);
break;
default:
break;
}
return style;
}
上一篇: C#实现IP摄像头的方法
下一篇: Android联网更新应用
推荐阅读
-
使用NPOI设置Excel表的单元格背景颜色
-
C#使用NPOI设置Excel单元格背景颜色(xls,xlsx)
-
python xlwt设置单元格的自定义背景颜色
-
php导出excel图片格式,PHPExcel API接口用法大全,按模板导入excel,美化excel,导出图片,设置单元格字体颜色背景色边框,合并单元格,设置行高列宽...
-
POI 设置单元格背景颜色【区分格式 xls、xlsx】
-
Excel2010中设置单元格批注格式如背景颜色、边框线条等等
-
python xlwt如何设置单元格的自定义背景颜色
-
NPOI 自定义设置单元格背景颜色[RGB格式]
-
Excel2010中设置单元格批注格式如背景颜色、边框线条等等