2013-07-15 32 views
11

Làm cách nào để thay đổi màu phông chữ tiêu đề thành màu trắng và tô màu xanh lục? Đây là các lớp mà tôi đang sử dụng:Làm cách nào để thay đổi Màu Văn bản và Fillcolor

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 

Và đây là mã mà tôi tin rằng nó sẽ được chèn vào.

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) 

Trả lời

9

Nếu bạn muốn đặt màu thành kiểu ô đơn giản ... bạn có thể viết mã như thế nào.

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); 

này sẽ lấp đầy các tế bào với màu xanh và font sẽ là màu TRẮNG Bold

+0

Tệp jar nào là XSSFCellStyle nằm trong? Điều này trông giống như những gì tôi cần! –

+0

Tôi đang sử dụng apache poi 3.9. XSSFCellStyle nằm trong org.apache.poi.xssf.usermodel.XSSFCellStyle – Sankumarsingh

+0

Tôi liệt kê nhập org.apache.poi.xssf.usermodel.XSSFCellStyle ở đầu tài liệu của tôi nhưng tôi nhận được "không thể phân giải lớp org.apache.poi .xssf.usermodel.XSSFCellStyle "message. Điều này có nghĩa là tôi không có nó? –

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

Đối với tập tin xls Tôi đã kiểm tra sau và đang làm việc tốt trên kết thúc của tôi.

Tôi cần phải nhập khẩu như sau:

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; 

và đoạn mã được như sau:

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(); 

Kết quả của mã này là ô đầu tiên của Row đầu tiên của tờ tại chỉ số hai, sẽ hiển thị văn bản "Văn bản tiêu đề" với màu màu xanh lục và Màu văn bản có màu trắng với Phông chữ đậm.

Tôi đang sử dụng apache POI 3.9.

Các vấn đề liên quan