2013-07-15 110 views
11

如何將標題字體顏色更改爲白色並填充綠色?這些是我正在使用的類:如何更改文本顏色和填充顏色

import static org.apache.poi.ss.usermodel.CellStyle.* 
import static org.apache.poi.ss.usermodel.IndexedColors.* 
import org.apache.poi.hssf.usermodel.* 
import org.apache.poi.hssf.usermodel.HSSFWorkbook 
import org.apache.poi.ss.usermodel.Cell 
import org.apache.poi.ss.usermodel.CellStyle 
import org.apache.poi.ss.usermodel.Row 
import org.apache.poi.ss.usermodel.Sheet 
import org.apache.poi.ss.usermodel.Workbook 
import org.apache.poi.ss.usermodel.Font 

而這是我相信它將被插入的代碼。

Font headerFont = wb.createFont(); 
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD) 
CellStyle headerStyle = wb.createCellStyle() 
headerStyle.setFont(headerFont) 

cellMOPID.setCellStyle(headerStyle) 
cellType.setCellStyle(headerStyle) 
cellStatus.setCellStyle(headerStyle) 
cellState.setCellStyle(headerStyle) 
cellStartDate.setCellStyle(headerStyle) 
cellEndDate.setCellStyle(headerStyle) 
cellDesc.setCellStyle(headerStyle) 

回答

9

如果您想將顏色設置爲簡單的cellstyle ...您可以編寫類似的代碼。

cell.setCellValue("Header Text"); 
XSSFCellStyle headerStyle = wb.createCellStyle(); 
Font headerFont = wb.createFont(); 
headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); 
headerFont.setColor(IndexedColors.WHITE.getIndex()); 
headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
headerStyle.setFont(headerFont); 
cell.setCellStyle(headerStyle); 

這將填補細胞與綠顏色和字體將是醒目白顏色

+0

哪個jar文件位於XSSFCellStyle中?這看起來像我需要的! –

+0

我使用的是Apache poi 3.9。 XSSFCellStyle位於org.apache.poi.xssf.usermodel.XSSFCellStyle – Sankumarsingh

+0

我列出導入org.apache.poi.xssf.usermodel.XSSFCellStyle在我的文檔的頂部,但我得到一個「無法解析類org.apache.poi .xssf.usermodel.XSSFCellStyle「消息。這是否意味着我沒有? –

11
 HSSFCellStyle cellStyle = workBook.createCellStyle();   
    HSSFFont font = wb.createFont(); 
    font.setFontName(XSSFFont.DEFAULT_FONT_NAME); 
    font.setFontHeightInPoints((short)10); 
    font.setColor(IndexedColors.BLUE.getIndex()); 
    cellStyle.setFont(font); 
    //the version i am using is poi-3.8 
8

爲xls文件的我已經檢查了以下,並在我結束工作的罰款。

我需要導入以下:

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.CellStyle; 
import org.apache.poi.ss.usermodel.Font; 
import org.apache.poi.ss.usermodel.IndexedColors; 

和一段代碼以下:在索引2

FileInputStream fin = new FileInputStream (XLSFileAddress); 
    HSSFWorkbook wb = new HSSFWorkbook(fin); 
    HSSFCell cell=wb.getSheetAt(2).getRow(0).getCell(0); 
    cell.setCellValue("Header Text"); 
    Font headerFont = wb.createFont(); 
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); 
    CellStyle headerStyle = wb.createCellStyle(); 
    headerStyle.setFont(headerFont); 
    headerStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex()); 
    headerFont.setColor(IndexedColors.WHITE.getIndex()); 
    headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
    cell.setCellStyle(headerStyle); 
    FileOutputStream fileOut = new FileOutputStream(XLSFileAddress); 
    wb.write(fileOut); 
    fileOut.close(); 

此代碼的輸出是片材的第一行的第一個單元,將顯示文本「標題文本」單元格顏色爲綠色,文本顏色爲白色並帶有粗體字體

我使用的是Apache POI 3.9。