java excel 字体颜色,APACHE POI得到确切的字体颜色从Excel中的Java

In excel sheet how to get the exact Font color value using Apache POI in java. I tried to get font color by using

org.apache.poi.ss.usermodel.Font f = book.getFontAt(style.getFontIndex());

short clrIdx = f.getColor();

but it is not giving the exact color index. After getting this color value i have to apply the same color in the PDFtable. Here, I am doing the excel to pdf conversion by reading each excel cell format and creating the same using pdf in iText.

Please help me!!

Thanks in advance.

解决方案

You need to get the RGB values from the Excel font color. You can get those values in a couple of steps.

To get the appropriate POI Color object, you need to go down the HSSF or XSSF path, extract the appropriate HSSFColor or XSSFColor, then get the RGB values out of the color.

int red = 0;

int green = 0;

int blue = 0;

if (font instanceof HSSFont)

{

HSSFColor color = ((HSSFFont) font).getHSSFColor(hssfWorkbook);

// 0: red, 1: green, 2: blue

short[] rgb = color.getTriplet();

red = rgb[0];

green = rgb[1];

blue = rgb[2];

}

else if (font instanceof XSSFFont)

{

XSSFColor color = ((XSSFFont) font).getXSSFColor();

byte[] rgb = color.getRgb();

// Bytes are signed, so values of 128+ are negative!

// 0: red, 1: green, 2: blue

red = (rgb[0] < 0) ? (rgb[0] + 256) : rgb[0];

green = (rgb[1] < 0) ? (rgb[1] + 256) : rgb[1];

blue = (rgb[2] < 0) ? (rgb[2] + 256) : rgb[2];

}

// Use the rgb values here.

Then you can use the rgb values to create your BaseColor object in iText.

Update:

There are several Apache POI bugs filed related to extracting colors in XSSF (for .xlsx files):

These bugs show up when XSSF is dealing with theme colors.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值