本文介绍了在 Apache POI 中为 XSSFWorkbook 设置自定义字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为 Apache POI 中的 XSSFWorkbook 设置自定义字体颜色时遇到了一些麻烦.当我这样做时:

I'm having a little trouble with setting a custom font color for an XSSFWorkbook from Apache POI. When I do:

    yellow = workbook.createCellStyle();
    Font whiteFont = workbook.createFont();
    whiteFont.setColor(new XSSFColor(new Color(255, 255, 255)).getIndexed());
    yellow.setFillForegroundColor(new XSSFColor(yellowRGB));
    yellow.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
    yellow.setFont(whiteFont);

字体保持黑色,但我不确定我做错了什么.

The font stays black, I'm not sure what I'm doing wrong though.

推荐答案

你可以这样做 whiteFont.setColor(new XSSFColor(new Color(255,255,255)));

但是,Apache POI 中有一个错误,正在切换黑白.看起来他们在 XSSFColor.java(查看 XSSFColor.correctRGB())以纠正 Excel 中的问题.Excel 可能已修复,但 Apache POI 未更新.

However, there is a bug in Apache POI, where it is switching black and white. It looks like they put a 'fix' in XSSFColor.java (look at XSSFColor.correctRGB()) to correct for a problem in Excel. It's likely that Excel was fixed, but Apache POI wasn't updated.

你可以这样做:whiteFont.setColor(HSSFColor.WHITE.index)whiteFont.setColor(IndexedColors.WHITE.index);(已弃用)

Instead you can do: whiteFont.setColor(HSSFColor.WHITE.index)or whiteFont.setColor(IndexedColors.WHITE.index); (this is deprecated)

或者如果你做 whiteFont.setColor(new XSSFColor(new Color(255,255,254))); 它会非常接近白色.

or if you do whiteFont.setColor(new XSSFColor(new Color(255,255,254))); it'll be really close to white.

这篇关于在 Apache POI 中为 XSSFWorkbook 设置自定义字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:58