关于easyPOI导出Excel功能

来源:互联网 发布:java服务器架构 编辑:程序博客网 时间:2024/05/24 07:32

最近做项目有导出Excel功能用的是easyPOI,下面总结过程

1.首先在pom.xml文件中添加

<dependency><groupId>org.jeecg</groupId><artifactId>easypoi-base</artifactId><version>2.4.0</version></dependency><dependency><groupId>org.jeecg</groupId><artifactId>easypoi-web</artifactId><version>2.4.0</version></dependency><dependency><groupId>org.jeecg</groupId><artifactId>easypoi-annotation</artifactId><version>2.4.0</version></dependency>

需要注意的是:版本号 apache.poi的版本号与easyPOI的对应版本,不然做不到导出.

@RequestMapping(value = "/daochuex")public void daoChuEx(@RequestParam("ids") String[] ids, HttpServletRequest request, HttpServletResponse response)throws Exception {TemplateExportParams params = new TemplateExportParams("static/excel/doc/逾期客户表-催收中.xlsx");Map<String, Object> map = new HashMap<>();String[] loannid = ids;List<CallLoanInfoDto> infolist = null;if (loannid != null && 0 != loannid.length) {Map<String, Object> infoid = new HashMap<>();infoid.put("callnid", loannid);infolist = loanTaskUnderServiceImpl.infolist(infoid);}System.out.println("=======================>" + infolist.toString());map.put("infolist", infolist);Workbook workbook = ExcelExportUtil.exportExcel(params, map);OutputStream out = null;try {out = response.getOutputStream();String fileName = "逾期客户表-催收中.xls";response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));workbook.write(out);} catch (Exception e) {e.printStackTrace();} finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}}

项目poi版本本来很低是后改成3.16;

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.16</version></dependency>

导入成功后等待包加入成功,

然后就是在Controller层写方法即可,方法我的方法无返回;

@RequestMapping(value = "/daochuex")public void daoChuEx(@RequestParam("ids") String[] ids, HttpServletRequest request, HttpServletResponse response)throws Exception {TemplateExportParams params = new TemplateExportParams("static/excel/doc/逾期客户表-催收中.xlsx");Map<String, Object> map = new HashMap<>();String[] loannid = ids;List<CallLoanInfoDto> infolist = null;if (loannid != null && 0 != loannid.length) {Map<String, Object> infoid = new HashMap<>();infoid.put("callnid", loannid);infolist = loanTaskUnderServiceImpl.infolist(infoid);}System.out.println("=======================>" + infolist.toString());map.put("infolist", infolist);Workbook workbook = ExcelExportUtil.exportExcel(params, map);OutputStream out = null;try {out = response.getOutputStream();String fileName = "逾期客户表-催收中.xls";response.setContentType("application/x-msdownload");response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));workbook.write(out);} catch (Exception e) {e.printStackTrace();} finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}}

页面:

$("#daochu").click(function() {var arrChk = $("input[name='subBox']:checked");var daochu = new Array();$(arrChk).each(function() { //each() 遍历函数daochu.push(this.id)});if (arrChk.length != 0) {layer.confirm('确认导出Excel?',{btn : [ '是的', '取消' ]},function() {window.location.href = "taskdetails/daochuex?ids="+ daochu;layer.msg('正在导出请稍等。。。',{icon : 1,time : 500})layer.load(0, {shade : false,time : 2000}, function() {layer.msg('导出成功')});}, function() {layer.msg('已取消导出', {icon : 2,time : 1000})})} else if (arrChk.length == 0) {layer.msg('勾选任务后再进行导出Excel', {icon : 5,time : 2000})}});});



原创粉丝点击