2014-03-27 572 views
7

我們新開始使用NPOI組件。ICellStyle FillForegroundColor自定義顏色比提供的命名顏色

我們有問題需要設置ICellStyle屬性的FillForegroundColor。

ICellStyle HeaderCellStyle = xssfworkbook.CreateCellStyle(); 

HeaderCellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index; 

FillForegroundColor期望類型短。

我們如何設置不同的顏色,而不是在HSSFColor中使用顏色。

我們需要設置"RGB192:0:0"以及我們如何做到這一點的ICellStyle財產FillForegroundColor

Colud有人幫助我們通過一些例子嗎?

回答

9

我發現了我自己的解決方案。請參考下面的代碼

byte[] rgb = new byte[3] { 192, 0, 0 }; 
XSSFCellStyle HeaderCellStyle1 = (XSSFCellStyle)xssfworkbook.CreateCellStyle(); 
HeaderCellStyle1.SetFillForegroundColor(new XSSFColor(rgb)); 
+2

鑄造我的風格'XSSFCellStyle'是所有我失蹤了。 –

0

改變字體顏色

例子..

ICellStyle style0 = hssfworkbook.CreateCellStyle(); 

IFont font0 = hssfworkbook.CreateFont(); 
font0.Color = NPOI.HSSF.Util.HSSFColor.RED.index; 
style0.SetFont(font0); 

for (int i = 0; i < cell.Length; i++) 
{ 
    cell[i].CellStyle = style0; 
} 

-

我是日本人,不能英語。抱歉。

2

對不起,再次。

Color SuperColor = Color.FromArgb(192, 0, 0); 
styleH.FillForegroundColor = GetXLColour(hssfworkbook, SuperColor); 


private short GetXLColour(HSSFWorkbook workbook, System.Drawing.Color SystemColour) 
{ 
    short s = 0; 
    HSSFPalette XlPalette = workbook.GetCustomPalette(); 
    NPOI.HSSF.Util.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; 
} 
+0

您是否有XSSFWorkbooks解決方案? – meme

1

對於人們來到這裏在2016年,你可以找到很多有用這裏顏色:

nameStyle.Color = NPOI.HSSF.Util.HSSFColor.BlueGrey.Index; 

藉助於此BlueGrey是你正在尋找的顏色。

1

這裏是一個VB等價的答案,我不得不轉換這一點,所以也許如果他們覺得這會節約一些工作:

'my workbook is from a template that already contains some header data 
Dim fs As New FileStream(HttpContext.Current.Server.MapPath("..\_ExcelTemplates\filename.xlsx"), FileMode.Open, FileAccess.Read)    
Dim workbook As IWorkbook 
workbook = New XSSFWorkbook(fs) 
Dim sheet_stats As ISheet = workbook.GetSheet("Stats") 

'style 
Dim rgb_grandTotalRow_fg() As Byte = New Byte() {197, 217, 241} 'lightish blue 
Dim style_grandTotalRow As XSSFCellStyle = workbook.CreateCellStyle() 
style_grandTotalRow.SetFillForegroundColor(New XSSFColor(rgb_grandTotalRow_fg)) 'XSSFCellStyle only for other use (FillForegroundColor = IndexedColors.LightTurquoise.Index) 
style_grandTotalRow.FillPattern = FillPattern.SolidForeground 

'apply the style to a cell 
Dim rowHdr As IRow = sheet_stats.GetRow(2) 
Dim cell As ICell 

cell = Row.CreateCell(1) 'create cell at col index 1 
cell.CellStyle = style_grandTotalRow 
cell.SetCellValue("TEST")