JXLS生成excel并自定义单元格样式
本人最近需要每天统计数据表,并每周一发送统计结果的邮件,所以写了个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并自定义单元格样式的更多相关文章
- PhpSpreadsheet生成Excel时实现单元格自动换行
PhpSpreadsheet是PHPExcel的替代版本,PHPExcel的作者已经停止了更新,今天尝试了使用PhpSpreadsheet生成Excel的时候支持单元格内的自动换行,发现用法其实差不多 ...
- asp.net+nopi生成Excel遇到设置单元格值null问题
Npoi 生成excel报表功能很不错,功能也不用给大家介绍了.首先看遇到的问题吧! FileStream file = new FileStream(Server.MapPath("Tem ...
- EXCEL设置选中单元格样式
你想这样啊,试试这段代码看行不:右键工作表名称--查看代码,在空白处粘贴就可以 Private Sub Worksheet_SelectionChange(ByVal Target As Range) ...
- POI生成EXCEL文件(字体、样式、单元格合并、计算公式)
创建一个封装类: package com.jason.excel; import java.io.FileNotFoundException; import java.io.FileOutputStr ...
- NPOI 生成Excel (单元格合并、设置单元格样式:字段,颜色、设置单元格为下拉框并限制输入值、设置单元格只能输入数字等)
NPIO源码地址:https://github.com/tonyqus/npoi NPIO使用参考:源码中的 NPOITest项目 下面代码包括: 1.包含多个Sheet的Excel 2.单元格合并 ...
- 用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法
本篇文章小编为大家介绍,用NPOI创建Excel.合并单元格.设置单元格样式.边框的方法.需要的朋友参考下 今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Ex ...
- java POI Excel 单元格样式
正如Html需要CSS一样,我们的POI生成的Excel同样需要样式才能更完美的表现我们的数据.下面还是从简单的例子出发,学习和了解POI的样式设计. 一.我的位置. 1 package com.my ...
- 创建excel,合并单元格,设置单元格样式
package com.huawei.excel; import java.io.File;import java.io.FileOutputStream;import java.util.Date; ...
- 导出excel带合并单元格方法的Demo
package com.test.util; import java.io.FileNotFoundException; import java.io.FileOutputStream; import ...
随机推荐
- php 微擎
pdo_insert('ewei_shop_member', $data); $my = array('agentid' => '4102'); // pdo_update(表明,'修改的值', ...
- (23)Spring Boot启动加载数据CommandLineRunner【从零开始学Spring Boot】
[Spring Boot 系列博客] )前言[从零开始学Spring Boot] : http://412887952-qq-com.iteye.com/blog/2291496 )spring bo ...
- 0622centos下coreseek安装及使用方法
Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和信息处理领域,适用于行业/垂直搜索.论坛 ...
- Remote Desktop安卓软件实现手机远程控制电脑
这篇文章写的是利用Remote Desktop安卓软件实现手机远程控制电脑. 电脑上的操作: 鼠标右击计算机>属性>远程设置>计算机名 如下图:
- libev与libuv的区别
参考: http://blog.csdn.net/w616589292/article/details/46475555 libuv 和 libev ,两个名字相当相近的 I/O Library,最近 ...
- log4js-Node.js中的日志管理模块使用与封装
开发过程中,日志记录是不可缺少的事情.尤其是生产系统中常常无法调试,因此日志就成了重要的调试信息来源. Node.js,已经有现成的开源日志模块,就是log4js,源代码地址:点击打开链接 项目引用方 ...
- 解决TortoiseGit下载代码每次要输入用户名、密码
解决办法: 方案1: 右键>ortoiseGit → Settings → Git → Credential 设置为 wincred - this repository only 或者 winc ...
- 【cl】本地安装maven的jar包报错Artifact is already in the local repository
原因是我直接把jar包放在了仓库里面 解决办法:将jar办放在不是仓库路径位置,在进行install就okle
- Android应用开发-护眼提醒-总结篇
设计初衷: 在学习<第一行代码>的服务那章时,涉及到了alarmmanager的内容.然后笔者当时正好在关注"程序猿怎样保护眼睛"的问题. 于是便自己做了一个demo, ...
- Qt5.8 提供 Apple tvOS,watchOS的技术预览版
New Platforms Apple tvOS (technology preview) Apple watchOS (technology preview) https://wiki.qt.io/ ...