主要使用easypoi中的ExcelExportEntity类对列进行封装,可以设置列的属性。

下面直接贴出自己的代码:(注意data数据类型List中的必须为map,不能为实体类,否则会报错)

pom文件依赖:

<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.1.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.1.0</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.1.0</version></dependency>

自己写的工具类:

/*** @author zhangz* @desc 简单excel动态选择列导出* @date 2020/6/16*/
public class ExcelExportUtils {private static Logger logger = LoggerFactory.getLogger(ConnUtil.class);/*** 选择列导出(单sheet)* @param fileNameUrl 文件导出服务器路径* @param sheetName sheet名称* @param dataList 数据list(map封装)* @param excelRows 导出的选择的列* @param title 标题(为空就传null)*/public static void exportExcel(String fileNameUrl, String sheetName, List<Map<String,Object>> dataList, List<ExcelExportDTO> excelRows,String title) throws Exception{// 使用easypoi中的ExcelExportEntity对象存储要导出的列List<ExcelExportEntity> entityList = new ArrayList<>();excelRows.forEach(item->{ExcelExportEntity exportEntity = new ExcelExportEntity(item.getLineName(), item.getFieldName());exportEntity.setHeight(item.getHeight());exportEntity.setWidth(item.getWidth());entityList.add(exportEntity);});// 执行方法ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);Workbook workbook = ExcelExportUtil.exportExcel(exportParams, entityList ,dataList);String fileName = fileNameUrl;File file = new File(fileName);try{FileOutputStream fout = new FileOutputStream(file);workbook.write(fout);fout.close();}catch (Exception e){logger.error("导出失败-----------------------------------");throw new Exception("导出失败!");}}/*** 多sheet导出* @param fileNameUrl 文件导出服务器路径* @param multiSheetDTOList 多sheet中的属性*/public static void exportMultiSheetExcel(String fileNameUrl, List<MultiSheetDTO> multiSheetDTOList) throws Exception{try{Workbook workbook = new HSSFWorkbook();// 遍历sheetfor (MultiSheetDTO multiSheetDTO:multiSheetDTOList){ExcelExportService server = new ExcelExportService();ExportParams exportParams = new ExportParams(multiSheetDTO.getTitle(), multiSheetDTO.getSheetName(), ExcelType.XSSF);List<ExcelExportEntity> entityList = new ArrayList<>();multiSheetDTO.getExcelRows().forEach(item->{ExcelExportEntity exportEntity = new ExcelExportEntity(item.getLineName(), item.getFieldName());exportEntity.setHeight(item.getHeight());exportEntity.setWidth(item.getWidth());entityList.add(exportEntity);});server.createSheetForMap(workbook, exportParams, entityList, multiSheetDTO.getDataList());}FileOutputStream fos = new FileOutputStream(fileNameUrl);workbook.write(fos);fos.close();}catch (Exception e){logger.error("导出失败-----------------------------------");throw new Exception("导出失败!");}}
}

依靠的一些其他类

ExcelExportDTO:
package com.tdh.light.spxt.api.domain.dto.excel;/*** @author zhangz* @desc 动态选择列参数* @date 2020/6/16*/
public class ExcelExportDTO {private String lineName;private String fieldName;private Double width = 20D;private Double height = 10D;public ExcelExportDTO(){}public ExcelExportDTO(String lineName, String fieldName) {this.lineName = lineName;this.fieldName = fieldName;}public ExcelExportDTO(String lineName, String fieldName, Double width, Double height) {this.lineName = lineName;this.fieldName = fieldName;this.width = width;this.height = height;}public String getLineName() {return lineName;}public void setLineName(String lineName) {this.lineName = lineName;}public String getFieldName() {return fieldName;}public void setFieldName(String fieldName) {this.fieldName = fieldName;}public Double getWidth() {return width;}public void setWidth(Double width) {this.width = width;}public Double getHeight() {return height;}public void setHeight(Double height) {this.height = height;}
}
MultiSheetDTO:
package com.tdh.light.spxt.api.domain.dto.excel;import java.util.List;
import java.util.Map;/*** @author zhangz* @desc 多sheet传参* @date 2020/6/16*/
public class MultiSheetDTO {/*** sheet名称*/private String sheetName;/*** 导出数据*/private List<Map<String,Object>> dataList;/*** 导出列属性*/private List<ExcelExportDTO> excelRows;/*** title*/private String title;public String getSheetName() {return sheetName;}public void setSheetName(String sheetName) {this.sheetName = sheetName;}public List<Map<String, Object>> getDataList() {return dataList;}public void setDataList(List<Map<String, Object>> dataList) {this.dataList = dataList;}public List<ExcelExportDTO> getExcelRows() {return excelRows;}public void setExcelRows(List<ExcelExportDTO> excelRows) {this.excelRows = excelRows;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}
}

这里面主要依靠了ExcelExportEntity 这个类来动态生成某个列

EasyPoi导出excel动态选择列相关推荐

  1. easypoi导出excel表格实现列宽自适应

    每日废话:每月5号发工资,但是每月感觉5号才是我最穷的一天,仔细想想,原来今天是资本家剥削压榨谈判的日子 //当前采用easypoi4.1.2版本依赖如下<!--excel 模板导出 easyp ...

  2. 使用easypoi导出excel实现动态列

    使用easypoi导出excel实现动态列 说明 使用的是easypoi进行导出 行头是动态生成 依据key进行列匹配,进行数据填充 第一列进行纵向动态合并 自己的一个使用,记录一下 工具依赖 < ...

  3. EasyPoi导出Excel实现标记颜色

    EasyPoi导出Excel实现标记颜色 PS:不知道EasyPoi 的可以看快速上手文档 <dependency><groupId>cn.afterturn</grou ...

  4. 使用EasyPOI导出Excel模板数据(含图片)

    使用EasyPOI导出Excel模板数据(含图片) EasyPOI功能如同名字Easy,主打的功能就是容易,让一个没接触过POI的人员可以方便的写出Excel导出,Excel模板导出,Excel导入, ...

  5. EasyPoi导出excel多Sheet遇到的坑

    问题描述 1.项目中需要多shee导出,需要动态生成列. 2.我的方法是在执行ExcelExportUtil.exportExcel之后,插入自定义的列. 3.发现在执行ExcelExportUtil ...

  6. 编码技巧——使用Easypoi导出Excel、多sheet

    本文主要介绍easypoi导出Excel的代码示例:自己之前手动实现过导出工具类<编码技巧--导出工具类>,基于实体和注解,通过反射来映射实体字段和exce列的关系:在部分工程里面看到了e ...

  7. easypoi导出EXCEL表格,WPS能打开,OFFICE打不开问题

    根据项目需求,需要导出excel表格,选择使用easypoi插件,可是导出的表格,选择wps能打开,office打开报错.于是就被测试提了一个问题单,(呜呜呜...)现在把解决方案分享下. 引入相关依 ...

  8. easypoi导出excel不设置样式_EasyPOI 导出excel设置边框,背景颜色,字体样式

    EasyPOI 导出excel设置边框,背景颜色,字体样式 EasyPOI 导出代码示例ExportParams exportParams = new ExportParams(); exportPa ...

  9. Easypoi 导出excel 使用注解实现一二级标题行的单元格合并

    Easypoi 导出excel 使用注解实现一二级标题行的单元格合并 先看一下最终效果图 上代码 Excel 模板实体类 @Data public class HxAdvisoryZJEndExcel ...

最新文章

  1. linux校园网客户端,Ubuntu Linux环境下校园网客户端安装使用
  2. python到底可以做什么-Python究竟是什么?能干嘛?
  3. python的就业方向和前景-2020年Python就业方向、就业前景分析
  4. 协程实现爬虫的例子主要优势在于充分利用IO时间去请求其他的url
  5. 大学生英语fif测试系统_英语听力解题技巧
  6. 线段树专辑——pku 3667 Hotel
  7. [react] react16的reconciliation和commit分别是什么?
  8. bootstrap中表格、修饰图片、浮动、背景框、提示框及关闭提示框、元素淡入淡出及jQuery中操作类名
  9. lnmp 1.4 mysql_lnmp1.4配置https教程
  10. 计算机应用基础模块2客观题答案 文档,卓顶精文2019计算机应用基础网上形考答案模块2 Word 2010 文字处理系统客观题答案...
  11. vue3 @/cli脚手架搭建项目
  12. codevs3143 二叉树的序遍历
  13. 国外工程师这样分析女人
  14. 计算机硬盘 u盘和光盘属于,磁盘U盘光盘的区别
  15. 利用波士顿房价数据集实现房价预测
  16. PHPWAMP开机自启异常,服务器重启后Apache等服务不会自启的原因分析
  17. 基于Python+Opencv实现改变logo颜色
  18. 12v电量显示制作方法_12V电池电量指示电路
  19. 计算机属性中没有端口,电脑设备管理器里没有显示COM2端口是什么原因
  20. oracle sqlt(sqltxplain) 诊断工具

热门文章

  1. WPF旋转科技感炫酷特效动画
  2. shou shen ji hua
  3. 网曝小伙手机贴膜月入3万 引发网友质疑 新辰深入调查真相
  4. python吃显卡还是内存不足_解决pytorch GPU 计算过程中出现内存耗尽的问题
  5. 小米平板机器怎么不Root激活XPOSED框架的步骤
  6. python实现md5算法_MD5算法详述及python实现2 zz
  7. 命令行下开启与关闭windows防火墙关端口(转)
  8. 常见报文格式(持续更细中)
  9. 宏基蜂鸟sf515装虚拟机运行linux,评测Acer/宏碁蜂鸟Swift5 SF515怎么样,15.6英寸轻薄商务办公笔记本电脑参数介绍!...
  10. 获取html表格指定行的元素