NPOIカスタムセル背景色[RGB形式]

5394 ワード

一.背景紹介
NPOIが持参する色が私たちの要求を満たすことができない場合、私たちは自分で背景色を定義する必要があり、NPOIの色タイプはshortタイプである.NetカラークラスはColorタイプですが、どのようにして互いに変換しますか?ネット上で1段のコードは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);

        /// 
        ///          
        /// 
        /// 
        /// 
        /// 
        /// 
        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;
        }

おすすめブログ:RyanDocデータ辞書For SqlServer版RyanCoderコードジェネレータFor SqlServer版