easyPOI 模板导出Excel

  • 步骤:

步骤:

  1. 依赖引入
<!-- Easypoi Excel导入导出工具 --><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.2.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.2.0</version></dependency>
  1. 导出工具类
package cn.afterturn.easypoi.excel;import java.util.Collection;
import java.util.List;
import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.ExcelBatchExportService;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil;public class ExcelExportUtil {private ExcelExportUtil() {}/*** @param entity*            表格标题属性* @param pojoClass*            Excel对象Class* @param dataSet*            Excel对象数据List*/public static Workbook exportBigExcel(ExportParams entity, Class<?> pojoClass,Collection<?> dataSet) {ExcelBatchExportService batchService = ExcelBatchExportService.getExcelBatchExportService(entity, pojoClass);return batchService.appendData(dataSet);}public static Workbook exportBigExcel(ExportParams entity, List<ExcelExportEntity> excelParams,Collection<?> dataSet) {ExcelBatchExportService batchService = ExcelBatchExportService.getExcelBatchExportService(entity, excelParams);return batchService.appendData(dataSet);}public static void closeExportBigExcel() {ExcelBatchExportService batchService = ExcelBatchExportService.getCurrentExcelBatchExportService();if(batchService != null) {batchService.closeExportBigExcel();}}/*** @param entity*            表格标题属性* @param pojoClass*            Excel对象Class* @param dataSet*            Excel对象数据List*/public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass,Collection<?> dataSet) {Workbook workbook = getWorkbook(entity.getType(),dataSet.size());new ExcelExportService().createSheet(workbook, entity, pojoClass, dataSet);return workbook;}private static Workbook getWorkbook(ExcelType type, int size) {if (ExcelType.HSSF.equals(type)) {return new HSSFWorkbook();} else if (size < 100000) {return new XSSFWorkbook();} else {return new SXSSFWorkbook();}}/*** 根据Map创建对应的Excel* @param entity*            表格标题属性* @param entityList*            Map对象列表* @param dataSet*            Excel对象数据List*/public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList,Collection<?> dataSet) {Workbook workbook = getWorkbook(entity.getType(),dataSet.size());;new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet);return workbook;}/*** 一个excel 创建多个sheet* * @param list*            多个Map key title 对应表格Title key entity 对应表格对应实体 key data*            Collection 数据* @return*/public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) {Workbook workbook = getWorkbook(type,0);for (Map<String, Object> map : list) {ExcelExportService service = new ExcelExportService();service.createSheet(workbook, (ExportParams) map.get("title"),(Class<?>) map.get("entity"), (Collection<?>) map.get("data"));}return workbook;}/*** 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理* * @param params*            导出参数类* @param pojoClass*            对应实体* @param dataSet*            实体集合* @param map*            模板集合* @return*/@Deprecatedpublic static Workbook exportExcel(TemplateExportParams params, Class<?> pojoClass,Collection<?> dataSet, Map<String, Object> map) {return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, pojoClass, dataSet,map);}/*** 导出文件通过模板解析只有模板,没有集合* * @param params*            导出参数类* @param map*            模板集合* @return*/public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) {return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, null, null, map);}/*** 导出文件通过模板解析只有模板,没有集合* 每个sheet对应一个map,导出到处,key是sheet的NUM* @param params*            导出参数类* @param map*            模板集合* @return*/public static Workbook exportExcel(Map<Integer, Map<String, Object>> map,TemplateExportParams params) {return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, map);}}

下载工具类

package com.hzrys.atplatform.finance.utils;import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;public class FileUtil {public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){ExportParams exportParams = new ExportParams(title, sheetName);exportParams.setCreateHeadRows(isCreateHeader);defaultExport(list, pojoClass, fileName, response, exportParams);}public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));}public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){defaultExport(list, fileName, response);}private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);if (workbook != null){downLoadExcel(fileName, response, workbook);}}public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {try {response.setCharacterEncoding("UTF-8");response.setHeader("content-Type", "application/vnd.ms-excel");response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));workbook.write(response.getOutputStream());} catch (IOException e) {e.printStackTrace();}}private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);if (workbook != null){downLoadExcel(fileName, response, workbook);}}public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){if (StringUtils.isBlank(filePath)){return null;}ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);List<T> list = null;try {list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);}catch (NoSuchElementException e){//("模板不能为空");e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return list;}public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){if (file == null){return null;}ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);List<T> list = null;try {list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);}catch (NoSuchElementException e){//("excel文件不能为空");e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return list;}
}

3.Controller实现

 /*** 导出余额列表Excel** @return*/@GetMapping("api/finance/actbalance/excelexport")@ApiOperation(httpMethod = "GET", value = "导出余额列表Excel")public void actBalanceExcelExport(@Valid ActBalanceQryDto actBalanceQryDto, HttpServletResponse response) {TemplateExportParams params = new TemplateExportParams(actBalanceQryDto.isNumDisplay() ? "exceltemplate/科目余额表-数量.xls" : "exceltemplate/科目余额表.xls");//excel导出数据Map<String, Object> map = actBalanceService.actBalanceExcelExport(actBalanceQryDto);Workbook workbook = ExcelExportUtil.exportExcel(params, map);FileUtil.downLoadExcel("kmye.xls", response, workbook);}

xls模板 单元格内容取自easyPOI命令

easyPOI 模板导出Excel相关推荐

  1. easypoi模板导出excel以及遇到的合并问题

    文章目录 前言 一.模板导出的关键词 二.模板填写 三.导出模板合并问题 四 结尾部分 总结 前言 背景是甲方要导出周报,但是导出的周报的样式比较复杂,只能选用模板导出,但是第一次使用模板导出遇到了一 ...

  2. 使用EasyPoi利用excel模板导出excel表格下载

    前言:使用excel模板导出excel的好处在于可以事先在模板上定义颜色.格式等,适用于模板设计得比较灵活复杂的场景 一.添加jar包 <dependency><groupId> ...

  3. 使用EasyPoi根据模板导出Excel或word文档

    接着上篇文章 Java根据模板导出Excel并生成多个Sheet 简单介绍下EasyPoi的使用,直接上代码吧 首先当然是先引入jar包了,看下图 其次,还是贴代码吧看实例,下面是根据模板导出的工具类 ...

  4. EasyPOI 根据模板导出excel时,无法自适应行高得解决方案记录

    目录 EasyPOI 根据模板导出excel时,无法自适应行高得解决方案记录 首先说一下问题得场景 第一步 第二步 第三步 第四步 第五步 第六步 第七步 (就是这个方法!) EasyPOI 根据模板 ...

  5. 使用EasyPoi导入导出Excel

    easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板 语言( ...

  6. 使用easypoi导入导出excel,SSM和SpringBoot通用代码

    easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板语言(熟悉 ...

  7. EasyPoi模板导出word(简单文本、内嵌表格、循环图片)

    EasyPoi模板导出word(简单文本.内嵌表格.循环图片) 先上效果图 模板在此 依赖导入 *坑:注意依赖版本 4.3.0+才支持多图片循环导出 <!-- word导出 方式:easypoi ...

  8. .Net NPOI 根据excel模板导出excel、直接生成excel

    一.根据Excel模板导出excel 1.导入NPOI.dll  2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...

  9. ​WeihanLi.Npoi 根据模板导出Excel

    WeihanLi.Npoi 根据模板导出Excel Intro 原来的导出方式比较适用于比较简单的导出,每一条数据在一行,数据列虽然自定义程度比较高,如果要一条数据对应多行就做不到了,于是就想支持根据 ...

  10. springboot使用jxls导出excel___(万能通用模板)--- SpringBoot导入、导出Excel文件___SpringBoot整合EasyExcel模板导出Excel

    springboot使用jxls导出excel 实现思路: 首先在springBoot(或者SpringCloud)项目的默认templates目录放入提前定义好的Excel模板,然后在具体的导出接口 ...

最新文章

  1. 机器人行业研究报告:智能化造就新时代,自动化生产成刚需
  2. 运放使用中不稳定怎么办?
  3. 开发出高性能的网站系列文章
  4. SPC.NET,为5年的开发做个结尾
  5. C++shell sort希尔排序的实现算法之二(附完整源码)
  6. jvm 错误_JVM因“ OutOfMemory”错误而关闭-我该怎么办?
  7. 论文学习8-How Question Generation Can Help Question Answering over Knowledge Base(KBQA-知识问答)
  8. python实现批量更改xml文件中内容替换
  9. 4位大佬解读:“医疗人工智能、信息化、政策与科研”的新风向与新趋势
  10. 设计模式 - 抽象工厂模式
  11. fu7推挽胆机音质_fu7电子管功放电路图大全(6N8P\6P3P\胆机功放电路\耦合电容器) - 全文...
  12. qt中clicked()和toggled()的区别
  13. css标题样式_CSS的标题集样式
  14. 【干货】从QQ群起家的情趣商城站长之路
  15. 计算机app无法删除,文件夹删不掉怎么办?
  16. 对于圆桌理论和经典概率判断算法的分析(转)
  17. shell脚本--杀死进程
  18. (3)登录界面——登录
  19. 用Linux开发板制作智能音箱,【工程师实战】只要几步,普通音箱秒变小度智能音箱...
  20. 国家c语言二级机试题,国家二级计算机考试题目C语言机试题库.doc

热门文章

  1. 【杂项】vmware fusion 流畅度设置
  2. 健康体检信息系统源码、医院体检源码 医院管理系统源码
  3. 在 KITTI 数据集中利用车辆位姿真值拼接 pcd 点云并滤波,得到一个准确的点云地图
  4. pth转onnx:RuntimeError: Exporting the operator uniform to ONNX opset version 9 is not supported.
  5. 使用活字格制作企业办公用品管理软件
  6. theano 全连接代码
  7. xposed框架android4,Xposed框架app_Xposed模块_xposed框架安卓4.4.4版-多特软件站安卓网...
  8. 教你查看网页的记住密码和免费下载付费音乐
  9. Matlab计算轮廓内切圆
  10. 开源房产中介管理系统