NPOI 颜色设置

来源:互联网 发布:mysql 显示视图前几行 编辑:程序博客网 时间:2024/06/05 23:46

NPOI颜色设置,NPOI颜色变化不固定

NPOI 内置了很多颜色,但我们经常想用自己的颜色,网上有GetXColor 方法,可以获取RGB颜色相对应的索引,用过了之后总是有些问题,生成出来的颜色是随机的,会随着环境而改变。所以,据我的理解,我们设置的颜色必须是NPOI内置的颜色

 

另外文档里面给出了很多颜色填充模式,找了半天才知道默认我们正常模式就是

1.GetXColor 方法,以失败而告终
   Dim styleChargePart_Title As NPOI.SS.UserModel.CellStyle = xlbook.CreateCellStyle()              
   styleChargePart_Title.FillBackgroundColor = Bill_201302_inditex.GetXLColour(xlbook, Color.FromArgb(153, 255, 204))
   Public Shared Function GetXLColour(ByVal xlWorkbook As NPOI.HSSF.UserModel.HSSFWorkbook, ByVal SystemColour As System.Drawing.Color) As Short
        Dim XlPalette As HSSFPalette = xlWorkbook.GetCustomPalette()
        Dim XlColour As NPOI.HSSF.Util.HSSFColor = XlPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B)
        'Available colour palette entries: 65 to 32766 (0-64=standard palette; 64=auto, 32767=unspecified)
        If IsNothing(XlColour) Then
            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()
            End If

        Else
            Return XlColour.GetIndex()
        End If
    End Function

2.还是乖乖用系统内置的颜色吧,另给出颜色列表

我喜欢把所有样式放在hashtable里面,到时用的时候按名字取

                Dim styleChargePart_Title As NPOI.SS.UserModel.CellStyle = xlbook.CreateCellStyle()
                styleChargePart_Title.SetFont(font12Bold)
                styleChargePart_Title.Alignment = HorizontalAlignment.CENTER
                styleChargePart_Title.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.YELLOW.index
                
                styleChargePart_Title.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.YELLOW.index
                styleChargePart_Title.FillPattern = FillPatternType.SOLID_FOREGROUND
                styleHash.Add("styleChargePart_Title", styleChargePart_Title)

 

3.模板内置颜色

有些时候我们导出的Excel是用的现有的excel做模板,将数据导入到模板里面,但这些模板的颜色也必须是NPOI内置的颜色,不然导出来的excel颜色有时还是随机的,你下载下来的表格第一次和第二次颜色可能一样

 

NPOI内置颜色列表

 

0 0
原创粉丝点击