首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >APACHE从Java的excel获得精确的字体颜色

APACHE从Java的excel获得精确的字体颜色
EN

Stack Overflow用户
提问于 2012-07-16 09:22:22
回答 1查看 6.1K关注 0票数 0

在excel表中,如何使用java中的Apache获得精确的字体颜色值。我试图通过使用

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

但它并没有给出确切的颜色指数。在得到这个颜色值之后,我必须在PDFtable中应用相同的颜色。在这里,我做的excel到pdf转换,通过读取每个excel单元格格式,并创建相同的使用pdf在iText。

请救救我!

提前谢谢。

EN

Stack Overflow用户

发布于 2013-01-15 21:40:40

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

要获得适当的POI Color对象,您需要沿着HSSF或XSSF路径,提取适当的HSSFColor或XSSFColor,然后将RGB值从颜色中提取出来。

代码语言:javascript
运行
复制
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文件):

#51222

#51236

#52079

#53274

当XSSF处理主题颜色时,这些bug就会出现。

票数 4
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11501434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档