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 ...
随机推荐
- 游标后面select 带有in时
今天遇到一个问题,使用游标时,在给游标填充值的时候,select 语句中带有 where查询条件,并且还有 in子句. 本来我是这样写的,试了很多次都不出结果,当然number in (304010 ...
- 邓_ SVN·最新使用教程总结
SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Subversion是什么? ...
- 实用的 鼠标滑上显示提示信息的jq插件
使用非常简单, 引用 css js文件, 将需要显示提示信息的元素 添加class="tooltip"类名, 在title属性填写提示信息就好了title="啊啊啊啊&q ...
- hdu 2089 数位dp入门题
#include<stdio.h> //dp[i][0]代表不存在不吉利数字 //dp[i][1]代表不存在不吉利数字但是以2开头 //dp[i][2]代表存在不吉利数字 #define ...
- HDU 3987 && DINIC
很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样: 求一次网络流最小割后,把满流的边置1,不满流的置INF.再求一次最大流即可. 为什么呢? 是否会存在一些边当前不满流 ...
- 生成字符Banner
生成字符Banner http://patorjk.com/software/taag __ _______/ |_ ____ ____ ____ / ___/\ __\/ _ \ / \ / _ \ ...
- Ruby 读取文件
Ruby 读取文件 一次全读出来 textAll = File.read("fileName.txt") puts textAll 一次读取一行 file = File.open( ...
- solr实战-(一)
实现用户数据索引及查询 1. 启动solr solr start 2. 创建collection solr create -c user 3. schema中加入field ...
- js动态创建表格------Day59
刚刚不知道怎么回事,CSDN博客一直打不开,就在博客园完毕了今天的记录,结果临关机,登录了下.发现又好了,就再多花个几分钟转下吧,也无论到底在意的是什么了,权当强迫症了... 前几天记录了动态的加入一 ...
- 转四种常见的post请求
HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交数据,本文 ...