- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个example jXLS 1.x 的动态单元格样式,但我找不到比 this example 更接近的东西对于 AreaListener。
我有一个非常基本的XLS生成模板,处理代码就这么简单
context.putVar("headers", columns);
context.putVar("data", cells);
context.getConfig().setCellStyleMap();
JxlsHelper.getInstance().processTemplate(is, result, context);
如何添加一些允许我修改某些单元格样式的监听器(例如为长度超过 N 个字符的文本添加自动换行,或者如果值具有特定模式则更改背景颜色)?
最佳答案
你可以这样实现
在主要方法中:
try(InputStream is = HighlightDemo.class.getResourceAsStream("highlight_template.xls")) {
try (OutputStream os = new FileOutputStream("target/highlight_output.xls")) {
PoiTransformer transformer = PoiTransformer.createTransformer(is, os);
AreaBuilder areaBuilder = new XlsCommentAreaBuilder(transformer, false);
List<Area> xlsAreaList = areaBuilder.build();
Area mainArea = xlsAreaList.get(0);
Area loopArea = xlsAreaList.get(0).getCommandDataList().get(0).getCommand().getAreaList().get(0);
loopArea.addAreaListener(new HighlightCellAreaListener(transformer));
Context context = new Context();
context.putVar("employees", employees);
mainArea.applyAt(new CellRef("Result!A1"), context);
mainArea.processFormulas();
transformer.write();
}
}
此示例中使用的模板与 Object Collection Demo 中的相同样本。最棘手的部分是找到您要应用 AreaListener 的区域。在这种情况下,我只是从根区域遍历到 EachCommand 区域,我希望在该区域突出显示付款超过 2000 的员工。
AreaListener 实现类似于 AreaListener example 中的实现
public class HighlightCellAreaListener implements AreaListener {
private final CellRef paymentCell = new CellRef("Template!C4")
...
public void afterTransformCell(CellRef srcCell, CellRef targetCell, Context context) {
System.out.println("Source: " + srcCell.getCellName() + ", Target: " + targetCell.getCellName());
if(paymentCell.equals(srcCell)){ // we are at employee payment cell
Employee employee = (Employee) context.getVar("employee");
if( employee.getPayment().doubleValue() > 2000 ){ // highlight payment when >= $2000
logger.info("highlighting payment for employee " + employee.getName());
highlightCell(targetCell);
}
}
}
private void highlightCell(CellRef cellRef) {
Workbook workbook = transformer.getWorkbook();
Sheet sheet = workbook.getSheet(cellRef.getSheetName());
Cell cell = sheet.getRow(cellRef.getRow()).getCell(cellRef.getCol());
CellStyle cellStyle = cell.getCellStyle();
CellStyle newCellStyle = workbook.createCellStyle();
newCellStyle.setDataFormat( cellStyle.getDataFormat() );
newCellStyle.setFont( workbook.getFontAt( cellStyle.getFontIndex() ));
newCellStyle.setFillBackgroundColor( cellStyle.getFillBackgroundColor());
newCellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
newCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(newCellStyle);
}
关于excel - jXLS 2.3 - 添加监听器以根据单元格内容更新单元格样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310928/
我为 jxls 2.x 创建了一个 Excel 模板。有一个单元格我应用了条件格式。但是,条件格式仅适用于我创建的完全相同的单元格,并且不会复制到以下行。任何的想法? 最佳答案 有一个与此相关的错误,
我可以在 jxls 1.X 中使用 varStatus 属性获取行索引,同时使用 foreach 遍历列表,但是 jxls 2.3.0 不支持这个,如何使用 jxls 2.3.0 在 EXCEL 中输
使用 JXLS 1,可以通过简单调用来在 Excel 模板中进行替换 XLSTransformer.transformXLS(is,beans) 例如,我们有 bean 对象,它们的组织方式如下(不太
我正在使用 jxls-poi-1.0.12。 jxls 2.5 发布后,gradle 自动将 jxls 更新为 2.5。对于结果,我在运行 JxlsHelper.getInstance().proce
我正在创建一个报告,我需要将我的数据结构输出到 Excel 电子表格。为此,我使用 JXLS,但在创建 jx 公式以正确输出数据时遇到问题。 我有一个人员类型列表: List people = ne
我尝试在第一个 jXLS 模板中评估像 ${messages.${column}} 这样的内部变量,但没有成功。 有人可以帮忙吗?这甚至是可能的吗?任何帮助将不胜感激。 干杯乔斯卡 最佳答案 任何对象
我想提供一种通过命令指定工作表名称的方法。工作表名称应根据工作表中的内容动态创建。 我注意到,当使用each-command的multisheet属性时,可以为动态工作表指定固定工作表名称。 在 ve
我只是想证明来自 Java JXLS 的演示代码:http://jxls.sourceforge.net/samples/object_collection.html但是在添加(没有 MAVEN)必要
如果我需要调用从多个线程创建 xls 文件的方法 - 我可以使用 XLSTransformer 作为同步(或者甚至不使用) )类的字段?如果 XLSTransformer.transformXLS()
我正在尝试将两个集合放入 Excel 文档中。我正在这样做: context.putVar("collection1", collection1); context.putVar("collectio
我正在使用 JXLS 创建 Excel 工作表。在模板中,我指定了每列所需的列宽。输出是 HSSFWorkbook。我从 HSSFWorkbook 对象创建一个字节数组,如下所示。问题是,我失去了列宽
我需要在 jxls bean 中使用 DateFormat 对象。如果在我的类里面我写了以下内容: private synchronized DateFormat df = new SimpleDat
我正在使用带有 apache poi 实现的 JXLS 2.3.0。 我使用以下代码来创建 excel : try{ InputStream is = ObjectCollectionDemo.
我使用 JXLS 和 Excel 模板创建了一个报告。我有一堆单元格/列从数据库中提取数据,然后有几列是空白的,然后我的报告中的几列中也有一些公式/条件。我遇到的问题是其中一个公式需要运行/乘以两个空
按照其网站上的示例,http://jxls.sourceforge.net/getting_started.html 。我在尝试写入处理后的文件时遇到了问题。它没有写入应由 JEXL 替换的数据,而是
我想使用一个库来读取 xls 文件,JXLS看起来非常有用,因为可以在 XML 文件中配置映射。 如果我遇到非常大的文件的性能问题,是否可以限制内存中的行数,以便一次读取文件 N 行? 谢谢。 最佳答
我正在使用 JXLS 使用自定义模板生成 Excel 文件。该模板只是库附带的标准网格导出模板,只是我更改了模板文件中某些列的宽度。我的代码大部分只是示例代码的副本。 private void ex
我正在尝试使用 jXLS 将数据从列表导出到 Excel 工作表。我需要使用 jXLS 创建一个 Excel 模板并使用该模板打印出数据列表。我有一个名为 Department 的 Bean 类,我需
使用过的版本 兴趣点:3.15 jxls:2.4.0 jxls-poi:1.0.12 这是一个template允许简单地重现问题。 请注意,我正在使用 JXLS 的多页功能。 当if-conditio
这是我面临的问题:嵌套集合。 我有一个 ArrayList (顶级集合)类。在每个 Subject 中,我都有另一个 Group 类型的 ArrayList。最后,我在每个组中都有一个 学生 的 Ar
我是一名优秀的程序员,十分优秀!