如何更改文本颜色和填充颜色

15

我该如何将标题字体颜色改为白色,背景填充为绿色?这是我正在使用的类:

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)
3个回答

16
     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

14
如果您想将颜色设置为简单的单元格样式...您可以编写类似以下代码的代码。

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

这将用绿色填充单元格,字体将是加粗的白色


2
问题可能是因为您正在使用HSSF和xls文件。我正在使用XSSF和xlsx文件。如果它不会影响您的要求,请转移到xlsx文件并使用XSSF。如果您需要xls文件,则保持在HSSF上。也许https://dev59.com/ZGgv5IYBdhLWcg3wBMIK#10924483可以帮助您解决问题。 - Sankumarsingh
没有特定的内存问题,无需从HSSF迁移到XSSF。您也可以使用HSSF应用字体颜色。 - swamy
@swamy - 你能用上面的原始代码给一个例子吗? - Jonathan Morningstar
顺便问一下,Jonathan...你现在使用的POI版本是哪个? - Sankumarsingh
我正在使用3.9版本。 - Jonathan Morningstar
显示剩余4条评论

9

对于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;

以下是相关的代码:

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

这段代码的输出结果是索引为2的工作表中第一行的第一个单元格,显示文本"Header Text"并且背景颜色为绿色,文字颜色为白色且加粗。我使用的是Apache POI 3.9版本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接