/// <summary> /// 获取RGB对应NPOI颜色值 /// </summary> /// <param name="workbook">当前wb</param> /// <param name="R"></param> /// <param name="G"></param> /// <param name="B"></param> /// <returns></returns> public static short GetXLColour(this HSSFWorkbook workbook, int R, int G, int B) { short s = 0; HSSFPalette XlPalette = workbook.GetCustomPalette(); HSSFColor XlColour = XlPalette.FindColor((byte)R, (byte)G, (byte)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((byte)R, (byte)G, (byte)B); } else { XlColour = XlPalette.FindSimilarColor((byte)R, (byte)G, (byte)B); } s = XlColour.GetIndex(); } } else { s = XlColour.GetIndex(); } return(s); }
/// <summary> /// create the colours for the COBie sheet /// </summary> /// <param name="colourName"></param> /// <param name="red"></param> /// <param name="green"></param> /// <param name="blue"></param> private void CreateColours(string colourName, byte red, byte green, byte blue) { IColor colour = null; if (IsXlsx) { byte[] rgb = new byte[3] { red, green, blue }; colour = new XSSFColor(rgb) as IColor; } else { HSSFPalette palette = ((HSSFWorkbook)ExcelWorkbook).GetCustomPalette(); colour = palette.FindSimilarColor(red, green, blue); if (colour == null) { // First 64 are system colours //srl this code does not work with the latest version of NPOI //if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 64 ) //{ // NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE = 64; //} //NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE++; colour = palette.AddColor(red, green, blue); } } _colours.Add(colourName, colour); }
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); }
/// <summary> /// 设置单元格样式 /// </summary> /// <param name="rownum"></param> /// <param name="isFill">是否填充背景色</param> /// <returns></returns> //private HSSFCellStyle SetStyle(int rownum,bool isFill) //{ // if (isFill) // { // if (rownum%2 == 0) // { // cellstyle.FillForegroundColor = HSSFColor.LIGHT_ORANGE.index; // } // else // cellstyle.FillForegroundColor = HSSFColor.SKY_BLUE.index; // cellstyle.FillPattern = FillPatternType.SOLID_FOREGROUND; // //cellstyle.FillBackgroundColor = HSSFColor.RED.index; // } // return cellstyle; //} /// <summary> /// 获取颜色 /// </summary> /// <param name="sysColor"></param> /// <returns></returns> private short GetColor(System.Drawing.Color sysColor) { short s = 0; HSSFPalette xlPalette = WorkBook.GetCustomPalette(); HSSFColor xlColor = xlPalette.FindColor(sysColor.R, sysColor.G, sysColor.B); if (xlColor == 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; xlColor = xlPalette.AddColor(sysColor.R, sysColor.G, sysColor.B); } else { xlColor = xlPalette.FindSimilarColor(sysColor.R, sysColor.G, sysColor.B); } s = xlColor.GetIndex(); } } else { s = xlColor.GetIndex(); } return(s); }
private short GetXLColour(System.Drawing.Color SystemColour) { HSSFPalette XlPalette = document.GetCustomPalette(); NPOI.HSSF.Util.HSSFColor XlColour = XlPalette.FindColor(SystemColour.R, SystemColour.G, SystemColour.B); XlColour = XlColour ?? XlPalette.AddColor(SystemColour.R, SystemColour.G, SystemColour.B); return(XlColour.Indexed); }
public void TestAddColor() { try { HSSFColor hssfColor = hssfPalette.AddColor((byte)10, (byte)10, (byte)10); Assert.Fail(); } catch (Exception) { // Failing because by default there are no colours left in the palette. } }
private void CreateColours(string colourName, byte red, byte green, byte blue) { HSSFPalette palette = XlsWorkbook.GetCustomPalette(); HSSFColor colour = palette.FindSimilarColor(red, green, blue); if (colour == null) { // First 64 are system colours //srl this code does not work with the latest version of NPOI //if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 64 ) //{ // NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE = 64; //} //NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE++; colour = palette.AddColor(red, green, blue); } _colours.Add(colourName, colour); }
private HSSFColor GetColor(HSSFWorkbook workbook, byte red, byte green, byte blue) { HSSFPalette palette = workbook.GetCustomPalette(); HSSFColor colour = palette.FindSimilarColor(red, green, blue); if (colour == null) { // NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE is now const... //// First 64 are system colours //if (NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE < 64) //{ // NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE = 64; //} //NPOI.HSSF.Record.PaletteRecord.STANDARD_PALETTE_SIZE++; colour = palette.AddColor(red, green, blue); } return(colour); }