NPOI custom cell background color [RGB format]

Source: Internet
Author: User

I. background when NPOI color does not meet our requirements, we need to define the background color, and NPOI color type is short type. net Color classes are of the Color type. How can they be converted to each other? There is a piece of code on the Internet that vb can solve the above problem. I translate it into C #, so that you can use VB: Private Function GetXLColour (ByVal SystemColour As System. drawing. color) As Short 'lookup RGB from. NET system color 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 color 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;} usage: Color LevelOneColor = Color. fromArgb (1, 143,176,229); Color LevelTwoColor = Color. fromArgb (1, 201,217,243); Color LevelThreeColor = Color. fromArgb (231,238,248); Color LevelFourColor = Color. fromArgb (1, 232,230,231); Color LevelFiveColor = Color. fromArgb (250,252,213 ); /// <summary> /// set the cell style in layers /// </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); // call the GetXLColour method 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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.