在excel表中,如何使用java中的Apache获得精确的字体颜色值。我试图通过使用
org.apache.poi.ss.usermodel.Font f= book.getFontAt(style.getFontIndex());clrIdx = f.getColor();
但它并没有给出确切的颜色指数。在得到这个颜色值之后,我必须在PDFtable中应用相同的颜色。在这里,我做的excel到pdf转换,通过读取每个excel单元格格式,并创建相同的使用pdf在iText。
请救救我!
提前谢谢。
发布于 2013-01-15 21:40:40
您需要从Excel字体颜色中获取RGB值。您可以通过几个步骤获得这些值。
要获得适当的POI Color
对象,您需要沿着HSSF或XSSF路径,提取适当的HSSFColor或XSSFColor,然后将RGB值从颜色中提取出来。
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值在BaseColor
中创建iText对象。
更新:
有几个Apache错误与在XSSF中提取颜色有关(对于.xlsx文件):
当XSSF处理主题颜色时,这些bug就会出现。
https://stackoverflow.com/questions/11501434
复制相似问题