本人最近需要每天统计数据表,并每周一发送统计结果的邮件,所以写了个springboot老自动完成工作。项目地址为:https://github.com/707293891/springboot

   其中某些统计数据需要特殊标记:红色显示。

   如图:模版如下

   但是需要在某一处特殊显示为红色:如图

   

     现在写出实现过程:

   利用jxls的区域监听器实现.

    

Transformer transformer = TransformerFactory.createTransformer(getFileInputStream(),
new FileOutputStream(
new File(
Thread.currentThread().getContextClassLoader().
getResource("excelTemplates/result").getFile()+"/result.xls")));
XlsArea xlsArea=new XlsArea("Sheet1!A1:F3",transformer);
XlsArea employeeArea = new XlsArea("Sheet1!A3:F3", transformer);
employeeArea.addAreaListener(new SimpleAreaListener(employeeArea));
EachCommand eachCommand=new EachCommand("item","items",employeeArea);
xlsArea.addCommand("A3:F3", eachCommand);
Context context = new Context();
Map map=new HashMap();
// map.put("list",getData());
map.put("week",CalendarUtil.getWeekNum());
map.put("month",CalendarUtil.getMonth());
context.putVar("print", map);
context.putVar("items",list);
xlsArea.applyAt(new CellRef("Sheet1!A1"), context);
transformer.write();
  SimpleAreaListener实现如下:
  
package com.yinhai.yunwei.excel;

import com.yinhai.yunwei.yunwei.mapper.YunweiInfo;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.jxls.area.Area;
import org.jxls.area.XlsArea;
import org.jxls.common.AreaListener;
import org.jxls.common.CellRef;
import org.jxls.common.Context;
import org.jxls.transform.poi.PoiTransformer; import java.util.Map; /**
* @author 范超
* @version V1.0
* @Title SimpleAreaListener
* @Package jxls
* @Descript :TODO()
* @date : 2018/6/25 上午10:22
*/
public class SimpleAreaListener implements AreaListener {
private Area area;
PoiTransformer transformer;
public SimpleAreaListener(XlsArea xlsArea) {
this.area=xlsArea;
transformer= (PoiTransformer) xlsArea.getTransformer();
} @Override
public void beforeApplyAtCell(CellRef cellRef, Context context) { } @Override
public void afterApplyAtCell(CellRef cellRef, Context context) {
} @Override
public void beforeTransformCell(CellRef cellRef, CellRef cellRef1, Context context) { } @Override
public void afterTransformCell(CellRef cellRef, CellRef cellRef1, Context context) {
if (cellRef1.getCol()!=3&&cellRef1.getCol()!=5){
return;
}
Workbook workbook=transformer.getWorkbook();
Cell cell=workbook.getSheet(cellRef1.getSheetName()).getRow(cellRef1.getRow()).getCell(cellRef1.getCol());
CellStyle cellStyle=cell.getCellStyle();
Font font=workbook.createFont();
CellStyle resultCell=workbook.createCellStyle();
Object item=context.getVar("item");
//需要显示红色的条件
if(item!=null&&item instanceof YunweiInfo &&"****".equals(((YunweiInfo) item).getName())){
font.setColor(XSSFFont.COLOR_RED);
resultCell.setFont(font);
cell.setCellStyle(resultCell);
}
} }

    这样当满足条件时就会实现特殊的显示格式了。

    其中jxls版本为如下

    

<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls</artifactId>
<version>[2.4.3,)</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-poi</artifactId>
<version>[1.0.14,)</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-jexcel</artifactId>
<version>[1.0.6,)</version>
</dependency>
<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>[2.0.3,)</version>
</dependency>
 

JXLS生成excel并自定义单元格样式的更多相关文章

  1. PhpSpreadsheet生成Excel时实现单元格自动换行

    PhpSpreadsheet是PHPExcel的替代版本,PHPExcel的作者已经停止了更新,今天尝试了使用PhpSpreadsheet生成Excel的时候支持单元格内的自动换行,发现用法其实差不多 ...

  2. asp.net+nopi生成Excel遇到设置单元格值null问题

    Npoi 生成excel报表功能很不错,功能也不用给大家介绍了.首先看遇到的问题吧! FileStream file = new FileStream(Server.MapPath("Tem ...

  3. EXCEL设置选中单元格样式

    你想这样啊,试试这段代码看行不:右键工作表名称--查看代码,在空白处粘贴就可以 Private Sub Worksheet_SelectionChange(ByVal Target As Range) ...

  4. POI生成EXCEL文件(字体、样式、单元格合并、计算公式)

    创建一个封装类: package com.jason.excel; import java.io.FileNotFoundException; import java.io.FileOutputStr ...

  5. NPOI 生成Excel (单元格合并、设置单元格样式:字段,颜色、设置单元格为下拉框并限制输入值、设置单元格只能输入数字等)

    NPIO源码地址:https://github.com/tonyqus/npoi NPIO使用参考:源码中的 NPOITest项目 下面代码包括: 1.包含多个Sheet的Excel 2.单元格合并 ...

  6. 用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法

    本篇文章小编为大家介绍,用NPOI创建Excel.合并单元格.设置单元格样式.边框的方法.需要的朋友参考下 今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Ex ...

  7. java POI Excel 单元格样式

    正如Html需要CSS一样,我们的POI生成的Excel同样需要样式才能更完美的表现我们的数据.下面还是从简单的例子出发,学习和了解POI的样式设计. 一.我的位置. 1 package com.my ...

  8. 创建excel,合并单元格,设置单元格样式

    package com.huawei.excel; import java.io.File;import java.io.FileOutputStream;import java.util.Date; ...

  9. 导出excel带合并单元格方法的Demo

    package com.test.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import ...

随机推荐

  1. layer 使用教程

    http://layer.layui.com/ <!DOCTYPE html><html lang="en"><head> <meta c ...

  2. 【Android界面实现】SlidingMenu最新版本号使用具体解释

    转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 在非常久之前的一篇文章中,简单的介绍了一下开源项目SlidingMenu控件的使用,这一篇文章,将比較具体的 ...

  3. 18005 It is not ugly number

    18005 It is not ugly number 时间限制:2000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description ...

  4. 【cl】cmd相关命令

    cd  进入目录 dir  列出当前目录下的文件[在linux上是ls] e:  进入E盘 tab键可以快速进入目录

  5. Hadoop-2.2.0中文文档——Apache Hadoop 下一代 MapReduce (YARN)

    MapReduce在hadoop-0.23中已经经历了一次全然彻底地大修.就是如今我们叫的MapReduce 2.0 (MRv2) or YARN. MRv2的基本思想是把JobTracker分成两个 ...

  6. ubuntu16.04安装chrome谷歌浏览器

    按下 Ctrl + Alt + t 键盘组合键,启动终端. 输入以下命令: sudo wget http://www.linuxidc.com/files/repo/google-chrome.lis ...

  7. 备份SQL SERVER 2005数据库

  8. 使用 `ConfigMap` 挂载配置文件

    使用 ConfigMap 挂载配置文件 Intro 有一些敏感信息比如数据库连接字符串之类的出于安全考虑,这些敏感信息保存在了 Azure KeyVault 中,最近应用上了 k8s 部署,所以想把 ...

  9. Koa 中实现 chunked 数据传输

    有关于 Transfer-Encoding:chunked 类型的响应,参见之前的文章HTTP 响应的分块传输.这里看 Koa 中如何实现. Koa 中请求返回的处理 虽然官方文档有描述说明不建议直接 ...

  10. Linux Shell Scripting Cookbook 读书笔记 4

    正则, grep 1. 正则表达式  正则表达式  描述  示例 ^ 行起始标记  ^hell匹配以hell开头的行 $ 行尾标记  test$匹配以test结尾的行 . 匹配任意一个字符  hell ...