Apache的POI和放大器;颜色 [英] Apache POI & colors

查看:419
本文介绍了Apache的POI和放大器;颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题与Apache POI,一些帮助将是不错!

I've some problems with Apache POI, some help would be nice!!

我想删除Excel表格的子部分的颜色。
要做到这一点,我想的第一件事是呈现我想要清洁单元(让说,第一行的一项)和其颜色设置为白色:

I would like to remove the colors in a subpart of an Excel sheet. To do so, the first thing I tried is to render the cells I want to clean (let say the one of the first row only) and to set their color to white:

cell.getCellStyle().setFillForegroundColor(IndexedColors.WHITE.index);

但是,如果我这样做,未呈现某些单元格的颜色(让说第二排的一个)也被更改为白细胞。这很奇怪,因为这是改变细胞不具有相同的前景色,所以我想他们会有不同的风格...

But if i do that, the color of some cells that are not rendered (let say the one of the second row) are also changed to white cells. It's strange, because the cells that are changed do not have the same foreground color, so I thought they would have different styles ...

因此​​,我已经试过另一种方式:

Therefore, I've tried another way:

CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.WHITE.index);
cell.setCellStyle(style);

的想法,就是要避免更改可能共同在Excel工作表单元格的不同风格。事实上,它解决了色差问题,但如果我这样做,我松在Excel工作表中的不同的风格,而我只是想去掉一些颜色...

The idea, is to avoid changing a style that may be common to different cells in the Excel sheet. Actually, it solves the color problem, but if I do that, I loose the different styles in the Excel sheet, while I only want to remove some colors...

我使用的是通用的工作簿,和我读一个XLSX(带XLS同样的问题)
你知道该怎么办呢?
多谢,
问候,

I'm using a generic Workbook, and I'm reading a xlsx (same problem with a xls) Do you know how to do it ? Thx a lot, regards,

推荐答案

如果您要删除的颜色​​,那么很可能是有意义的改变填充图案 NO_FILL 而如下的颜色设置为白色:

If you want to remove the colors, then probably it makes sense to change the fill pattern to NO_FILL rather than setting the color to white as follows:

style.setFillPattern(CellStyle.NO_FILL)

返回到您的主要问题,你得处理单元格样式修改的2个选项:

Back to your main question, you've got 2 options of dealing with the cell style amendment:


  • 按通过 CellUtil.setCellStyleProperty 方法修改样式

  • By modifying style viaCellUtil.setCellStyleProperty method

CellUtil.setCellStyleProperty(电池,工作簿,CellUtil.FILL_PATTERN,CellStyle.NO_FILL)


  • 通过从现有的样式风格克隆和修改属性

  • By cloning styles from the existing style and modifying the property

下面的例子code:

 CellStyle oldStyle = cell.getCellStyle();
 CellStyle newStyle = workbook.createCellStyle();
 newStyle.cloneStyleFrom(oldStyle);
 newStyle.setFillPattern(CellStyle.NO_FILL);
 cell.setCellStyle(newStyle);

然而,在第二种情况,你要创建的每一个细胞一个新的单元格样式,你可能会考虑重新使用一些已经更新单元格样式和实施某种缓存(因为否则可能导致性能问题和允许达到极限的风格)。

However in the 2nd case you're going to create a new cell style for every cell and you might probably consider reusing some of the already updated cell styles and implementing some kind of caching (as otherwise it might lead to performance issues and reaching allowed styles limit).

这篇关于Apache的POI和放大器;颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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