午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java中EasyPoi多sheet導(dǎo)出功能實(shí)現(xiàn)

瀏覽:17日期:2022-08-18 13:58:47
EasyPoi 多sheet導(dǎo)出

序言:之前一直想開(kāi)始寫(xiě)博客,都沒(méi)有時(shí)間行動(dòng)起來(lái),今天終于開(kāi)始了我的第一篇博客…

最近接到一個(gè)導(dǎo)出excel功能的需求,該功能主要難點(diǎn)是

多sheet頁(yè) 導(dǎo)出合并單元格(跨行、跨列) 多表頭合并

我開(kāi)始的想法是如果采用poi來(lái)實(shí)現(xiàn)這個(gè)功能,業(yè)務(wù)邏輯可能會(huì)有點(diǎn)復(fù)雜,于是我使用了easyPoi——一個(gè)so easy的工具,它的特點(diǎn)就是非常方便,用jQuery的一句來(lái)說(shuō)就是:write Less,Do More。

話不多說(shuō),接下來(lái)分享一下我的代碼(我使用的是SSH框架搭建環(huán)境)

一、引入maven jar包

<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>二、編寫(xiě)導(dǎo)出的實(shí)體類(使用注解形式)

1、DeptUtil 類

@ExcelTarget('deptUtil')public class DeptUtil { @Excel(name = '部門(mén)編號(hào)', width = 30 , needMerge = true) private Integer id; @Excel(name = '部門(mén)名稱', width = 30 , needMerge = true) private String deptName; @ExcelCollection(name = '員工信息') private List<EmpUtil> emps;....省略getter、setter方法

2、EmpUtil類

@ExcelTarget('empUtil')public class EmpUtil{ @Excel(name = '序號(hào)', width = 30, isColumnHidden = true) private Integer id; @Excel(name = '員工姓名', width = 30, groupName = '基本信息') private String empName; @Excel(name = '年齡', width = 30, type = 10, groupName = '基本信息') private Integer age; @Excel(name = '入職時(shí)間', width = 30, groupName = '工作信息', format = 'yyyy/MM/dd HH:mm') private Date hiredate; @Excel(name = '薪酬', width = 30, type = 10, groupName = '工作信息') private BigDecimal salary;....省略getter、setter方法

3、核心代碼

public String export(){ Workbook workBook = null; try { List<DeptUtil> exportList = exportService.exportList(); System.err.println(JSONArray.toJSONString(exportList)); // 創(chuàng)建參數(shù)對(duì)象(用來(lái)設(shè)定excel得sheet得內(nèi)容等信息) ExportParams deptExportParams = new ExportParams(); // 設(shè)置sheet得名稱 deptExportParams.setSheetName('員工報(bào)表1'); // 創(chuàng)建sheet1使用得map Map<String, Object> deptExportMap = new HashMap<>(); // title的參數(shù)為ExportParams類型,目前僅僅在ExportParams中設(shè)置了sheetName deptExportMap.put('title', deptExportParams); // 模版導(dǎo)出對(duì)應(yīng)得實(shí)體類型 deptExportMap.put('entity', DeptUtil.class); // sheet中要填充得數(shù)據(jù) deptExportMap.put('data', exportList); ExportParams empExportParams = new ExportParams(); empExportParams.setSheetName('員工報(bào)表2'); // 創(chuàng)建sheet2使用得map Map<String, Object> empExportMap = new HashMap<>(); empExportMap.put('title', empExportParams); empExportMap.put('entity', DeptUtil.class); empExportMap.put('data', exportList); // 將sheet1、sheet2、sheet3使用得map進(jìn)行包裝 List<Map<String, Object>> sheetsList = new ArrayList<>(); sheetsList.add(deptExportMap); sheetsList.add(empExportMap); // 執(zhí)行方法 workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF); fileName = URLEncoder.encode('員工報(bào)表導(dǎo)出', 'UTF-8'); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workBook.write(outputStream); outputStream.flush(); byte[] byteArray = outputStream.toByteArray(); excelStream = new ByteArrayInputStream(byteArray,0,byteArray.length); outputStream.close(); }catch (Exception e){ e.printStackTrace(); }finally { if(workBook != null) {try { workBook.close();} catch (IOException e) { e.printStackTrace();} } } return 'success'; }三、效果展示

Java中EasyPoi多sheet導(dǎo)出功能實(shí)現(xiàn)

有一個(gè)問(wèn)題就是如果sheet填充的數(shù)據(jù)源是一樣的,那么第二個(gè)sheet的內(nèi)容就會(huì)為空

Java中EasyPoi多sheet導(dǎo)出功能實(shí)現(xiàn)

不過(guò)一般上要實(shí)現(xiàn)多sheet頁(yè)展示,內(nèi)容應(yīng)該是不一樣的,這里只是作為實(shí)現(xiàn)效果展示,就采用了同一個(gè)數(shù)據(jù)獲取源。

以上就是所有內(nèi)容了,如果想要學(xué)習(xí)更多可以查閱easyPoi的api(http://easypoi.mydoc.io/)另外附上我的項(xiàng)目源碼:https://github.com/wzqonly/easyPoi_export

到此這篇關(guān)于Java中EasyPoi多sheet導(dǎo)出功能實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java EasyPoi多sheet導(dǎo)出內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 99r精品| 91小视频在线 | 免费成人深夜天涯网站 | 一区二区三区欧美日韩 | 在线中文字幕视频 | 噼里啪啦国语完整在线观看高清 | 国内久久 | 神马午夜我不卡 | 黄色直接看 | 综合一区在线 | 亚洲综合成人在线 | 天天躁夜夜躁 | 亚洲午夜视频在线观看 | 久久精品99国产国产精 | 久草热在线视频 | 91美女在线 | 国产精品成人国产乱一区 | 久久久久久久久久久网站 | 色综合天天操 | 麻豆chinese极品少妇 | 国产精品成人在线视频 | 丁香六月婷婷 | 日韩精品一区在线 | 好看的中文字幕 | 美日韩中文字幕 | 四虎国产精品永久在线国在线 | 在线视频一区二区 | 日日麻批免费视频播放 | 超碰碰碰 | 日本精品久久久久 | 欧美午夜理伦三级在线观看 | 另类一区二区三区 | 国产精品手机视频 | 中出中文字幕 | 国产日韩欧美中文字幕 | 中文精品一区二区三区 | 国产黄色精品网站 | 17c国产精品一区二区 | 日韩国产在线 | 国产黄色影视 | 午夜精品在线播放 |