APACHE POI得到确切的字体颜色从Excel中的Java [英] APACHE POI getting exact font color from excel in Java

查看:2492
本文介绍了APACHE POI得到确切的字体颜色从Excel中的Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在excel表格中如何让在Java中使用Apache POI确切的字体颜色值。我试图用

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());
短clrIdx = f.getColor();

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

,但它不给出精确的颜色指数。得到这个颜色值后,我不得不申请在PDFtable相同的颜色。在这里,我通过阅读每个Excel单元格的格式,并创建使用PDF的iText中做相同的Excel以PDF转换。

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.

请帮助我!

先谢谢了。

推荐答案

您需要从Excel的字体颜色获得RGB值。您可以在几个步骤获取这些值。

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

要获得相应的POI 颜色对象,您需要下井HSSF或XSSF路径,提取相应的HSSFColor或XSSFColor,然后得到的RGB值出来的颜色。

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.

然后你可以使用RGB值来创建你的iText BaseColor 对象。

更新:

有XSSF中提交有关提取颜色(的.xlsx文件)几个Apache POI错误:

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

#51222

#51236

#52079

#53274

这些错误显示当XSSF正在处理的主题颜色。

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

这篇关于APACHE POI得到确切的字体颜色从Excel中的Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆