java - Apache POI 3.10。 XSFFONT 设置颜色不起作用

标签 java apache-poi

我尝试使用白色字体的单元格,但它们总是显示为黑色,这是我的代码:

HSSFCellStyle estiloCabecera = wb.createCellStyle();
estiloCabecera.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
estiloCabecera.setFillPattern(CellStyle.SOLID_FOREGROUND);

HSSFFont fuenteCabecera = wb.createFont();
fuenteCabecera.setFontHeightInPoints((short)12);
fuenteCabecera.setFontName("Arial");
fuenteCabecera.setColor((short)HSSFColor.WHITE.index);
//fuenteCabecera.setColor(HSSFFont.COLOR_RED);
estiloCabecera.setFont(fuenteCabecera);

Row rowCabeceraTabla = sheet.createRow((short)3);
Cell celda = rowCabeceraTabla.createCell(0);
celda.setCellValue(new HSSFRichTextString("Id pregunta"));
celda.setCellStyle(estiloCabecera);

我不明白为什么,我已经尝试了我能想到的一切。请问有人可以帮忙吗?

最佳答案

正如我的评论中所发布的,您的示例对我有用,即我得到一个带有白色文本的黑色单元格。

矩形而不是文本通常指出您遇到编码问题或字体不支持字符。您可以尝试使用“Arial Unicode MS”来代替“Arial”吗?

我将我的代码作为答案发布,仅供进一步引用...如果我们发现新的详细信息,我会更新它,这可以解决您的问题...

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;


public class ForegroundColor {
    public static void main(String[] args) throws Exception {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet();

        CellStyle estiloCabecera = wb.createCellStyle();
        estiloCabecera.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
        estiloCabecera.setFillPattern(CellStyle.SOLID_FOREGROUND);

        Font fuenteCabecera = wb.createFont();
        fuenteCabecera.setFontHeightInPoints((short)12);
        fuenteCabecera.setFontName("Arial");
        fuenteCabecera.setColor((short)HSSFColor.WHITE.index);
        //fuenteCabecera.setColor(HSSFFont.COLOR_RED);
        estiloCabecera.setFont(fuenteCabecera);

        Row rowCabeceraTabla = sheet.createRow((short)3);
        Cell celda = rowCabeceraTabla.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Id pregunta"));
        celda.setCellStyle(estiloCabecera);

        FileOutputStream fos = new FileOutputStream("color.xls"); 
        wb.write(fos);
        fos.close();
    }
}

关于java - Apache POI 3.10。 XSFFONT 设置颜色不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19483014/

相关文章:

java - 如何使用 Apache POI PPT API 设置文本字体?

java - 库中的 Google 广告商 ID

java - 如何使用 Java/Apache POI 获取文件摘要信息

java - POI SAX 日期数据类型

java - 使用 Apache POI 获取 Excel 文件中的列名称

java - Apache POI - JAVA - 遍历 excel 中的列

java - 更新 MutableLiveData 时可观察到的抛出异常

java - 仅限外部 IP 的 Tomcat SSL 重定向

java - Android JNI 检测到应用程序错误 : JNI GetMethodID called with pending exception

java - 未从 Account 类获取最新版本的 stripe 中的 stripePublishableKey