2013-08-21 38 views
1

我想從使用NPOI庫的vb.net應用程序創建Excel電子表格。我使用下面的代碼:使用NPOI庫創建電子表格並使用帶有彩色背景的單元格

Imports NPOI.HSSF.UserModel 
Imports NPOI.SS.UserModel 
Imports NPOI.SS.Util 
Imports NPOI.HSSF.Util 
Imports NPOI.POIFS.FileSystem 
Imports NPOI.HPSF 
Imports NPOI.HSSF.Util.HSSFColor 

'create a background color of red 
Dim styleCellRedBackGround As HSSFCellStyle = workbook.CreateCellStyle() 
styleCellRedBackGround.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.RED.index 
Dim styleCellRedBackGroundFont = workbook.CreateFont() 
styleCellRedBackGroundFont.FontName = "Red Background" 
styleCellRedBackGround.SetFont(styleCellRedBackGroundFont) 

row = sheet.CreateRow(2) 
cell = row.CreateCell(0) 
cell.CellStyle = styleCellRedBackGround 
cell.SetCellValue("<<COMPETITIVE REBATES>>") 

電子表格被創建,但沒有紅色背景的細胞。如果我創建一個單元格並指定紅色背景,但不要向單元格寫入任何內容,這似乎可行。似乎有關如何做到這一點的文件有限。有什麼建議麼?

感謝

回答

0

請設置styleCellRedBackGround.FillPattern = FillPattern.SOLID;

0

您可能希望在現有電子表格中創建樣式,使用NPOI打開它並查看CellStyle是如何定義的。這樣你就可以從你想要的事情開始,並可以從那裏創建相應的代碼。

相關問題