当前位置: 移动技术网 > IT编程>开发语言>.net > NPOI 自定义设置单元格背景颜色[RGB格式]

NPOI 自定义设置单元格背景颜色[RGB格式]

2018年09月20日  | 移动技术网IT编程  | 我要评论

胡书涵微博,伊莱克斯石靖,厦门户外

一.背景介绍   

 

                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;  

 }  

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网