easyexcel 设置标题_使用easyexcel完成复杂表头及标题的导出功能(自定义样式)
如需客户端指定excel版本,只需要判断后缀名然后在controller中的.excelType(ExcelTypeEnum.XLS)做指定输出内容格式即可
***(注意表格行高列宽统一设置是在实体类的类名注解上,如果需要对表格进行精细的宽高设置需要删除掉这两个注解,可以在拦截器使用row的方法进行设置)
1. ## 引入依赖
com.alibaba
easyexcel
2.1.4
2.实体类(注解法)
package com.jpxx.admin.pilebody.service.api.dto;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.alibaba.excel.util.StringUtils;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@NoArgsConstructor
@Accessors(chain = true)
@ContentRowHeight(45)
@HeadRowHeight(50)
public class PilebodycheckMonthDto {
@ExcelIgnore
private String id;
@ExcelIgnore
private String cityid;
@ExcelIgnore
private String districtid;
@ExcelProperty(value = {"序号","序号"},index = 0)
@ColumnWidth(10)
private String orderNum;
@ExcelProperty(value = {"堆体名称","堆体名称"},index = 1)
@ColumnWidth(15)
private String name;
@ExcelProperty(value = {"具体位置","具体位置"},index = 3)
@ColumnWidth(30)
private String address;
@ExcelProperty(value = {"占地面积(平方)","占地面积(平方)"},index = 4)
@ColumnWidth(15)
private String areastr;
@ExcelProperty(value = {"堆体高度(米)","堆体高度(米)"},index = 5)
@ColumnWidth(10)
private String heightstr;
@ExcelProperty(value = {"建筑垃圾堆存量(万方)","建筑垃圾堆存量(万方)"},index = 6)
@ColumnWidth(15)
private String stocknum;
@ExcelIgnore
@Dict(dicCode = "governway")
private String governway;
@ExcelProperty(value = {"治理方式","治理方式"},index = 7)
@ColumnWidth(20)
private String governwayname;
@ExcelProperty(value = {"如需外运,计划外运时间","如需外运,计划外运时间"},index = 8)
@ColumnWidth(15)
private String outwardtransporttime;
@ExcelProperty(value = {"截止目前累计治理量(万方)","截止目前累计治理量(万方)"},index = 13)
@ColumnWidth(15)
private String governnum;
@ExcelProperty(value = {"治理主体","治理主体"},index = 14)
@ColumnWidth(15)
private String governbody;
@ExcelIgnore
@Dict(dicCode = "typestr")
private String typestr;
@ExcelProperty(value = {"堆体类型","堆体类型"},index = 2)
@ColumnWidth(15)
private String typestrname;
@ExcelIgnore
@Dict(dicCode = "statestr")
private String statestr;
@ExcelIgnore
private String districtname;
@ExcelProperty(value = {"监管单位","监管单位"},index = 15)
@ColumnWidth(15)
private String supervisedepartname;
@ExcelProperty(value = {"监管责任人","监管责任人"},index = 16)
@ColumnWidth(10)
private String supervisepeoname;
@ExcelProperty(value = {"职务","职务"},index = 17)
@ColumnWidth(10)
private String supervisepeoposition;
@ExcelProperty(value = {"联系方式","联系方式"},index = 18)
@ColumnWidth(20)
private String supervisepeophone;
@ExcelIgnore
private String residuenum;
@ExcelIgnore
private String governendtime;
@ExcelIgnore
private String governendyearmonth;
@ExcelProperty(value = {"本月治理量(万方)","外运量"},index = 9)
@ColumnWidth(15)
private String outwardtransportnum;
@ExcelProperty(value = {"本月治理量(万方)","整理地形绿化量"},index = 10)
@ColumnWidth(15)
private String afforestnum;
@ExcelProperty(value = {"本月治理量(万方)","临时覆盖或绿化量"},index = 11)
@ColumnWidth(15)
private String temporarilynum ;
@ExcelProperty(value = {"本月治理量(万方)","合计"},index = 12)
private String goverytotal;
@ExcelIgnore
private String qynum;
@ExcelIgnore
@Dict(dicCode = "sourcestr")
private String sourcestr;
@ExcelIgnore
private String createbyname;
}
controller
@postMapping(“pilebodystatisticsmonthexport”)
public WebApiResponse pilebodystatisticsmonthexport (HttpServletResponse response,String month) throws IOException {
List pilebodysList = pilebodycheckService.pilebodystatisticsmonth(sysDepartDto, month);
//设置序号
for (int i = 1;i <= pilebodysList.size();i++){
pilebodysList.get(i-1).setOrderNum(i+"");
}
response.setContentType(“application/vnd.ms-excel”);
response.setCharacterEncoding(“utf-8”);
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode(“存量建筑垃圾堆体治理进度月报表”, “UTF-8”);
response.setHeader(“Content-disposition”, “attachment;filename=” + fileName + “.xls”);
//内容样式策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//垂直居中,水平居中
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设置 自动换行
contentWriteCellStyle.setWrapped(true);
// 字体策略
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short) 12);
contentWriteCellStyle.setWriteFont(contentWriteFont);
//头策略使用默认
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
//excel如需下载到本地,只需要将response.getOutputStream()换成File即可(注释掉以上response代码)
EasyExcel.write(response.getOutputStream(), PilebodycheckMonthDto.class)
//设置输出excel版本,不设置默认为xlsx
.excelType(ExcelTypeEnum.XLS).head(PilebodycheckMonthDto.class)
//设置拦截器或自定义样式
.registerWriteHandler(new MonthSheetWriteHandler())
.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle))
.sheet("存量建筑垃圾堆体治理进度月报表")
//设置默认样式及写入头信息开始的行数
.useDefaultStyle(true).relativeHeadRowIndex(3)
//这里的addsumColomn方法是个添加合计的方法,可删除
.doWrite(pilebodycheckService.addSumColomn(pilebodysList));
return new WebApiResponse(200, "生成excel文件成功", null);
}
4. 拦截器
package com.jpxx.admin.pilebody.web.api;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public class MonthSheetWriteHandler implements SheetWriteHandler {
@Override
public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
Workbook workbook = writeWorkbookHolder.getWorkbook();
Sheet sheet = workbook.getSheetAt(0);
Row row1 = sheet.createRow(0);
row1.setHeight((short) 500);
Cell cell = row1.createCell(0);
//设置单元格内容
cell.setCellValue("附件2");
//设置标题
Row row2 = sheet.createRow(1);
row2.setHeight((short) 800);
Cell cell1 = row2.createCell(0);
cell1.setCellValue("存量建筑垃圾堆体治理进度月报表");
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cellStyle.setAlignment(HorizontalAlignment.CENTER);
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeight((short) 400);
cellStyle.setFont(font);
cell1.setCellStyle(cellStyle);
sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, 17));
//设置填表日期,填报人,联系方式
Row row3 = sheet.createRow(2);
row3.setHeight((short) 500);
row3.createCell(1).setCellValue("填表日期");
row3.createCell(11).setCellValue("填表人");
row3.createCell(15).setCellValue("联系方式");
}
}
生成表格如下:
easyexcel 设置标题_使用easyexcel完成复杂表头及标题的导出功能(自定义样式)相关推荐
- 使用easyexcel完成复杂表头及标题的导出功能(自定义样式及多sheet导出)
如需客户端指定excel版本,只需要判断后缀名然后在controller中的.excelType(ExcelTypeEnum.XLS)做指定输出内容格式即可 ***(注意表格行高列宽统一设置是在实体类 ...
- easyexcel 在 设置标题_七. EasyExcel标题加批注和标题字体填充红色
一, 概述 在日常开发中, 经常会碰到导入导出的场景, 有导入就肯定有导入模板, 本文将介绍利用EasyExcel给标题添加批注和挑剔字体填充颜色 二. 代码 2.1 编写样式处理类: TitleHa ...
- easyexcel多个sheet导入_Easypoi实现excel多sheet表导入导出功能
Easypoi简化了开发中对文档的导入导出实现,并不像poi那样都要写大段工具类来搞定文档的读写. 第一步引入Easypoi依赖 cn.afterturn easypoi-spring-boot-st ...
- ultravnc 设置代理_云立方IP丨IP代理可以实现哪些功能?
原标题:云立方IP丨IP代理可以实现哪些功能? 代理IP也就是说的代理服务器,主要功能就是安全的保护用户,它主要工作在开放系统互联(OSI)模型的对话层,从而起到防火墙的作用.大多数的代理服务器会被用 ...
- 500个爆文标题_看了1000个爆文标题,终于发现了这个规律.......
文章要爆,标题要好,这不仅仅是一句口号而已! 爆文标题有何套路,什么样标题在自媒体领域更受欢迎?小易MM吭哧吭哧的从易撰爆文库搬来了1000个爆文标题数据做研究,今天就一起与大家来说说爆文标题的那些事 ...
- easyexcel 导入指定_阿里巴巴EasyExcel使用(3)-导入
/*** 读取转换异常 **/ public class DemoExceptionListener extends AnalysisEventListener{private static fina ...
- python画图标题_使用pyplot.matshow()函数添加绘图标题
仅供参考 import matplotlib.pyplot as plt import numpy as np def samplemat(dims): """Make ...
- python xpath爬取新闻标题_爬取知乎热榜标题和连接 (python,requests,xpath)
用python爬取知乎的热榜,获取标题和链接. 环境和方法:ubantu16.04.python3.requests.xpath 1.用浏览器打开知乎,并登录 2.获取cookie和User-Agen ...
- SpringBoot中使用Easyexcel实现Excel导入导出功能(三)
导出的数据包含有图片 导出excel表格的数据包含有图片,这种场景比较少.通Easyexcel实现这样的需求,我认为最简便的方法就是使用前面提到的自定义转换器(com.alibaba.excel.co ...
最新文章
- cmake find_package opencv 找不到
- 你也能与AlphaGo谈笑风生:AlphaGo教学工具上线,2万多变化,37万多步棋
- 网站微信登录授权 ASP.NET
- python逐行读取json_如何用python读取json文件里指定的数据
- ubuntu18docker下安装MySQL
- Java针对ArrayList自定义排序的2种实现方法
- 阿里 Re-rank Recommendation 读后感
- 工作总结22:拦截器
- Java Collections singletonMap()方法与示例
- 【软件质量】CMM与CMMI
- java 获取音频文件时长
- iPhone 12或支持全新短距离WiFi标准 数据传输更快
- python3发布时间_什么时候python 3 才能有更好的支持呢?
- 有什么python在线编辑器-Python常用的编辑器有哪些?老男孩Python
- Java基础练习题---this
- (root用户)bash: ./xx: Permission denied解决方法
- 文本编辑器(1.0)
- 网络游戏外挂制作(3)-1
- tradingview教程 charting_library
- 详谈为什么互联网公司禁用外键约束
热门文章
- python 求出4行5列的二维数组周边元素之和
- diabetes影响因子2017_Journal of Diabetes
- 怎么做95置信区间图_这种动态的OD图怎么做?简单3步快速搞定
- python做什么模型_主题模型初学者指南[Python]
- java url 本地文件是否存在_我的应用程序知道URL中是否存在文件会一直停止[重复]...
- linux远程拷贝免手动输入密码,scp远程拷贝避免输入密码
- sqlserver date类型和字符串比较_基于SQL Server数据库搭建主从复制实现读写分离实战演练...
- python获取软件窗口句柄_Python获取浏览器窗口句柄过程解析
- php 接口日志,PHP 开发 APP 接口--错误日志接口
- python字符串百分号_Python字符串格式化的2种方法