EasyPOI之导出Excel复杂模板

  • 一、简介
  • 二、导出的excel格式
  • 三、项目中的maven依赖
  • 四、设计好生成的excel模板实体类
    • 1.导出对应的实体类(一对多中的“一”)
    • 2.导出对应的实体类(一对多中的“多”)
  • 五、controller层对需要的导出的数据生成格式
    • 1.对数据进行操作
    • 2.导出excel格式设置
  • 六、导出excel模板数据

一、简介

easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板语言(熟悉的表达式语法),完成以前复杂的写法.
1.添加依赖项
2.改造模型,添加注解
3.从数据库查询数据,使用Easypoi工具类生成Workbook对象,存储为文件(导出)。

二、导出的excel格式

三、项目中的maven依赖

<!--EasyPoi导入导出-->
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.3.0</version>
</dependency>
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.3.0</version>
</dependency>
<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.3.0</version>
</dependency>

easyexcel 和项目现版本的poi版本冲突解决办法:项目之前用的poi的版本是3.9,后来使用easyexcel的时候,出现某些类找不到,这是因为poi.jar包版本冲突导致的,maven会自动使用最高级的版本,而easyexcel是3.1.7,将项目依赖的poi升级到4.1.2就好了。

四、设计好生成的excel模板实体类

1.导出对应的实体类(一对多中的“一”)

@Data
/*** @author dz* @date 2021/2/21* @description 合同明细主表*/
@Data
@ExcelTarget("toalExcel")
public class ToalExcel {@ExcelEntity(id="ContractManagement")private ContractManagementExcel contractManagementExcel ;@ExcelCollection(name = "采购合同明细",orderNum="4")private List<PurchaseContractofProjectExcel> pexcel;@ExcelEntity(id="ContractManagement2")private ContractManagementExcel2 contractManagementExcel2 ;@ExcelCollection(name = "销售合同明细",orderNum="2")private List<SalesContractofProjectExcel> sexcel;@ExcelEntity(id="ContractManagement3")private ContractManagementExcel3 contractManagementExcel3 ;}
@Data
/*** @author dz* @date 2021/2/21* @description 合同明细主表-项目信息1*/
@Data
public class ContractManagementExcel {@Excel(name = "序号",needMerge = true)private String serialNumber;@Excel(name = "项目编号",needMerge = true)private String businessSn;@Excel(name = "项目名称",needMerge = true,width = 20)private String businessName;}
@Data
/*** @author dz* @date 2021/2/21* @description 合同明细主表-项目信息2*/
@Data
public class ContractManagementExcel2 {@Excel(name = "客户",needMerge = true)private  String customer;@Excel(name = "销售总金额",needMerge = true)private String forecastIncome;
}
 @Data/*** @author dz* @date 2021/2/21* @description 合同明细主表-项目信息3*/
@Data
public class ContractManagementExcel3 {@Excel(name = "税后净利润",needMerge = true)private String netProfitAfterTax;@Excel(name = "合同利润率",needMerge = true)private String contractProfitMargin;
}

2.导出对应的实体类(一对多中的“多”)

@Data
/*** @author dz* @date 2021/2/21* @description 合同明细主表-采购合同表*/
@Data
public class PurchaseContractofProjectExcel {@Excel(name = "供应商",width = 20)private  String supplier;@Excel(name = "采购成本",width = 15)private String procurementCosts;@Excel(name = "付款进度",width = 15)private String paymentSchedule;@Excel(name = "收票金额",width = 15)private String ticketAmount;
}
@Data
/*** @author dz* @date 2021/2/21* @description 合同明细主表-销售合同*/
public class SalesContractofProjectExcel {@Excel(name = "客户",width = 15)private String customer;@Excel(name = "销售金额",width = 15)private String salesAmount;@Excel(name = "收款进度",width = 15)private String collectionSchedule;@Excel(name = "开票金额",width = 15)private String invoiceAmount;
}

五、controller层对需要的导出的数据生成格式

1.对数据进行操作

/*** 导出表格数据*/@ApiOperationSupport(order = 5)@ApiOperation(value = "导出列表表格")@GetMapping("/export-company")@ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "要打印的数据id", dataType = "String", required = false)})public void exportCompanyceshi(HttpServletResponse response, String ids) throws Exception {List<Long> longs = Func.toLongList(ids);List<ToalExcel> list = new ArrayList<>();Integer num = 0;List<Map<String, Object>> projectVOList = exportExcelService.getprojectVOExcelList(longs);List<Map<String, Object>> businessVOList = exportExcelService.getbusinessVOExcelList(longs);List<Map<String, Object>> purchaseContractVOList = exportExcelService.getpurchaseContractVOExcelList(longs);List<Long> purchaseContractIds = new ArrayList<>();if (purchaseContractVOList != null) {for (Map map : purchaseContractVOList) {String str = String.valueOf(map.get("id"));Long id = Long.parseLong(str);purchaseContractIds.add(id);}}List<Map<String, Object>> purchasePaymentProcessVOList = exportExcelService.getpurchasePaymentProcessVOExcelList(purchaseContractIds);List<Map<String, Object>> receiptStatusEntityList = exportExcelService.getreceiptStatusEntityExcelList(purchaseContractIds);List<Map<String, Object>> salesContractVOList = exportExcelService.getsalesContractVOExcelList(longs);List<Long> salesContractIds = new ArrayList<>();if (salesContractVOList != null) {for (Map map : salesContractVOList) {String str = String.valueOf(map.get("id"));Long id = Long.parseLong(str);salesContractIds.add(id);}}List<Map<String, Object>> salesCollectionProcessEntityList = exportExcelService.getsalesCollectionProcessEntityExcelList(salesContractIds);List<Map<String, Object>> invoicingStatusEntityList = exportExcelService.getinvoicingStatusEntityExcelList(salesContractIds);for (int a = 0; a < longs.size(); a++) {String businessSn = "";String businessName = "";String customerName = "";String forecastIncome = "";String contractProfitMargin = "";String netProfitAfterTax = "";ContractManagementExcel contractManagementExcel = new ContractManagementExcel();List<PurchaseContractofProjectExcel> pExcel = new ArrayList<>();ContractManagementExcel2 contractManagementExcel2 = new ContractManagementExcel2();List<SalesContractofProjectExcel> sExcel = new ArrayList<>();ContractManagementExcel3 contractManagementExcel3 = new ContractManagementExcel3();//Map projectvo = projectVOList.get(a);forecastIncome = String.valueOf(projectVOList.get(a).get("forecastIncome"));contractProfitMargin = String.valueOf(projectVOList.get(a).get("contractProfitMargin"));netProfitAfterTax = String.valueOf(projectVOList.get(a).get("netProfitAfterTax"));//商机明细for (int b = 0; b < businessVOList.size(); b++) {if (projectVOList.get(a).get("id").equals(businessVOList.get(b).get("id"))) {businessSn = String.valueOf(businessVOList.get(b).get("businessSn"));businessName = String.valueOf(businessVOList.get(b).get("businessName"));customerName = String.valueOf(businessVOList.get(b).get("customerName"));}}contractManagementExcel.setSerialNumber(a+1+"");contractManagementExcel.setBusinessSn(businessSn);contractManagementExcel.setBusinessName(businessName);contractManagementExcel2.setCustomer(customerName);contractManagementExcel2.setForecastIncome(forecastIncome);contractManagementExcel3.setContractProfitMargin(contractProfitMargin);contractManagementExcel3.setNetProfitAfterTax(netProfitAfterTax);if (purchaseContractVOList!=null) {//采购合同明细for (int c = 0; c < purchaseContractVOList.size(); c++) {if (projectVOList.get(a).get("id").equals(purchaseContractVOList.get(c).get("projectId").toString())){PurchaseContractofProjectExcel p = new PurchaseContractofProjectExcel();String name = "";String amount = "";name = String.valueOf(purchaseContractVOList.get(c).get("customerName"));amount = String.valueOf(purchaseContractVOList.get(c).get("amount"));p.setSupplier(name);p.setProcurementCosts(amount);if (purchasePaymentProcessVOList != null) {String purchasePaymentProcessMessage = "";for (int e = 0; e < purchasePaymentProcessVOList.size(); e++) {String start="";String alias = "";String money = "";if (purchaseContractVOList.get(c).get("id").equals(purchasePaymentProcessVOList.get(e).get("purchaseContractId").toString())) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (purchasePaymentProcessVOList.get(e).get("finishTime") != null) {Date date = (Date) purchasePaymentProcessVOList.get(e).get("finishTime");start = sdf.format(date);}if (purchasePaymentProcessVOList.get(e).get("alias") != null) {alias = String.valueOf(purchasePaymentProcessVOList.get(e).get("alias"));}if (purchasePaymentProcessVOList.get(e).get("amount") != null) {money = String.valueOf(purchasePaymentProcessVOList.get(e).get("amount"));}purchasePaymentProcessMessage += start + "," + alias + "," + money + "\n";}}p.setPaymentSchedule(purchasePaymentProcessMessage);}if (receiptStatusEntityList != null) {String receiptStatusMessage = "";for (int f = 0; f < receiptStatusEntityList.size(); f++) {String start = "";String money = "";if (purchaseContractVOList.get(c).get("id").equals(receiptStatusEntityList.get(f).get("purchaseContractId").toString())) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (receiptStatusEntityList.get(f).get("createTime") != null) {Date date = (Date) receiptStatusEntityList.get(f).get("createTime");start = sdf.format(date);}if (receiptStatusEntityList.get(f).get("amount") != null) {money = String.valueOf(receiptStatusEntityList.get(f).get("amount"));}receiptStatusMessage += start + "," + money + "\n";}}p.setTicketAmount(receiptStatusMessage);}pExcel.add(p);}}}if (salesContractVOList!=null){//销售合同明细for (int d = 0; d < salesContractVOList.size(); d++) {if (projectVOList.get(a).get("id").equals(salesContractVOList.get(d).get("projectId").toString())) {SalesContractofProjectExcel s=new SalesContractofProjectExcel();String name="";String amount="";if (salesContractVOList.get(d).get("name")!=null){name=salesContractVOList.get(d).get("name").toString();}if (salesContractVOList.get(d).get("amount")!=null){amount=salesContractVOList.get(d).get("amount").toString();}s.setCustomer(name);s.setSalesAmount(amount);if (salesCollectionProcessEntityList!=null) {String salesCollectionProcessMessage = "";for (int g = 0; g < salesCollectionProcessEntityList.size(); g++) {String start = "";String bank = "";String money = "";if (salesContractVOList.get(d).get("id").equals(salesCollectionProcessEntityList.get(g).get("salesContractId").toString())){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (salesCollectionProcessEntityList.get(g).get("time") != null) {Date date = (Date) salesCollectionProcessEntityList.get(g).get("time");start = sdf.format(date);}if (salesCollectionProcessEntityList.get(g).get("bank") != null) {bank = String.valueOf(salesCollectionProcessEntityList.get(g).get("bank"));}if (salesCollectionProcessEntityList.get(g).get("amount") != null) {money = String.valueOf(salesCollectionProcessEntityList.get(g).get("amount"));}salesCollectionProcessMessage += start + "," + bank + "," + money + "\n";}}s.setCollectionSchedule(salesCollectionProcessMessage);}if (invoicingStatusEntityList!=null) {String invoicingStatusMessage = "";for (int h = 0; h < invoicingStatusEntityList.size(); h++) {String start = "";String money = "";SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");if (salesContractVOList.get(d).get("id").equals(invoicingStatusEntityList.get(h).get("salesContractId").toString())){if (invoicingStatusEntityList.get(h).get("finishTime") != null) {Date date = (Date) invoicingStatusEntityList.get(h).get("finishTime");start = sdf.format(date);}if (invoicingStatusEntityList.get(h).get("amount") != null) {money = String.valueOf(invoicingStatusEntityList.get(h).get("amount"));}invoicingStatusMessage += start + "," + money + "\n";}}s.setInvoiceAmount(invoicingStatusMessage);}sExcel.add(s);}}}ToalExcel toalExcel=new ToalExcel();toalExcel.setContractManagementExcel(contractManagementExcel);toalExcel.setContractManagementExcel2(contractManagementExcel2);toalExcel.setContractManagementExcel3(contractManagementExcel3);toalExcel.setSexcel(sExcel);toalExcel.setPexcel(pExcel);list.add(toalExcel);}// 简单模板导出方法ExportParams params = new ExportParams();params.setSheetName("合同管理");//设置sheet名params.setType(ExcelType.XSSF);Workbook workbook =   ExcelExportUtil.exportExcel(params, ToalExcel.class, list);//返回头设置下载,并设置文件名,返回setExportExcelFormat(response, workbook, "合同管理");}

2.导出excel格式设置

/*** excel导出时* 返回头设置下载,并设置文件名* * @param response* @param workbook* @param fileName* @throws Exception*/
private void setExportExcelFormat(HttpServletResponse response, Workbook workbook, String fileName) throws Exception {ServletOutputStream outStream = null;fileName = "合同明细.xlsx";//创建文件名(改)String fileNameURL = URLEncoder.encode(fileName, "UTF-8");response.setContentType("octets/stream");response.setHeader("Content-disposition", "attachment;filename=" + fileNameURL + ";" + "filename*=utf-8''" + fileNameURL);response.addHeader("Pargam", "no-cache");response.addHeader("Cache-Control", "no-cache");try {outStream = response.getOutputStream();workbook.write(outStream);} finally {outStream.close();}
}

六、导出excel模板数据


EasyPOI官方操作文档

EasyPOI之导出Excel复杂模板相关推荐

  1. JXLS导出Excel(模板导出)

    1.导包 在pom.xml中加入依赖如下: <dependency><groupId>org.jxls</groupId><artifactId>jxl ...

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

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 星悬月 来源 | blog.csdn.net/ ...

  3. bootstraptable导出excel独立使用_使用 EasyPOI 优雅导出Excel模板数据(含图片)

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

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

    作者:星悬月 blog.csdn.net/u012441819/article/details/96828044 EasyPOI功能如同名字Easy,主打的功能就是容易,让一个没接触过POI的人员可以 ...

  5. 使用EasyPoi导入导出Excel

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

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

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

  7. Easypoi模版导出excel

    若您正在使用easypoi进行excel导出的话,希望这篇文章能帮到您. 该文章是使用easypoi得模版方式实现excel导出(目前是单sheet的导出), 若需要使用非模版形式导出多sheet得话 ...

  8. C#读写导入导出Excel表格模板(NPOI)

    NPOI介绍: NPOI是指构建在POI 3.x版本之上的一个程序,NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作. NPOI是一个开源的C#读写Excel.WORD ...

  9. poi导出Excel之模板导出

    模板导出 实现流程1.自定义excel模板,放到Springboot的resource路径下,程序按照excel的路径将Excel读取成workbook流, 2.给单元格赋值,我们知道模板长什么样子, ...

  10. easypoi导出数值型_SpringBoot使用EasyPoi进行数据导入导出Excel(一)

    在实际项目开发中,对于Excel的导入导出还是很常见的需求,比如说将数据根据模板批量导入到数据库中,以及将数据库中的数据批量导出陈Excel的形式 现有需求: 下载固定的导入Excel模板 导入Exc ...

最新文章

  1. oracle的dual表
  2. matlab做数据间方差,用matlab做方差比检验的问题
  3. mysql特有语法_MySQL详细的基础语法
  4. Web API 2 入门——创建ASP.NET Web API的帮助页面(谷歌翻译)
  5. 表1 中的值 给表2
  6. UIKit 框架之UIAlertController
  7. 从输入url开始,完善前端体系架构
  8. 汽车金融平台百金贷宣布良性退出网贷业务
  9. Java服务器上显示图片问题_java,_服务器读取图片到jsp显示问题,java - phpStudy
  10. matlab中设置拟合初值,如何确定自定义函数拟合时的参数初值?
  11. 电源层和地线层完整性规则_射频电路设计实例以及一些经常遇见的问题
  12. php集成环境xampp完整安装过程
  13. 给JavaScript 初心者的ES2015 实战
  14. Mountainous landscape
  15. (CCF202109-4)收集卡牌(概率DP)
  16. Qt实现 QOpenGL绘制彩色三角形
  17. 安卓日记——手把手教你做知乎日报
  18. cesium从入门到进阶(一):Viewer、Scene、Camera、加载第三方影像、地形服务
  19. 【统计学】详解 A/B 测试
  20. D3D12渲染技术之渲染管线

热门文章

  1. 计算机中用于表示储存,计算机中用来表示存储器容量的基本单位是
  2. 软件测试方法--黑盒测试、白盒测试
  3. 深度学习相关软件安装整理
  4. flash动画转html5 效果,一键把SWF转HTML5 canvas动画的工具-Fanvas
  5. 小猪的Python学习之旅 —— 7.Python并发之threading模块(1)
  6. 什么是计算机病毒?中国黑客教父告诉你
  7. 2021年危险化学品经营单位安全管理人员考试报名及危险化学品经营单位安全管理人员作业考试题库
  8. iOS - AVAudioSession详解
  9. csf播放器(csf播放器ios)
  10. 内网渗透-域管理员定位