Java中EasyPoi導(dǎo)出復(fù)雜合并單元格的方法
前言:
上星期做了一個Excel的單元格合并,用的是EasyPoi,我之前合并單元格都是原生的,第一次使用EasyPoi合并也不太熟悉,看著網(wǎng)上自己套用,使用后發(fā)現(xiàn)比原生的方便些,貢獻(xiàn)一下,也給其他用到合并而且用的是EasyPoi的小伙伴節(jié)省下時間。
導(dǎo)出模板:

坐標(biāo):
版本號,自己來定,可以去官網(wǎng)查看:EasyPoi官網(wǎng)
<!-- easypoi 導(dǎo)入包 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.0.0</version>
</dependency>
實現(xiàn)代碼:
//表頭設(shè)置
List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>();
ExcelExportEntity colEntity = new ExcelExportEntity("經(jīng)銷商", "distributorName");
colEntity.setNeedMerge(true);
colEntity.setWidth(20);
colList.add(colEntity);
colEntity = new ExcelExportEntity("科室", "dept");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("部門", "region");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("省份", "province");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("門店數(shù)量", "storeNum");
colEntity.setNeedMerge(true);
colEntity.setStatistics(true);
colList.add(colEntity);
Map<String, Integer> map = DateUtils.getLastDayOfMonthByStr(request.getMonthStr());
Integer dayNum = map.get("dayNum");
for (int i = 1; i <= dayNum; i++) {
ExcelExportEntity group_1 = new ExcelExportEntity(i + "日", "day");
List<ExcelExportEntity> exportEntities = new ArrayList<>();
ExcelExportEntity appalyExcel = new ExcelExportEntity("申請數(shù)量", "applyNum" + i);
appalyExcel.setStatistics(true);
exportEntities.add(appalyExcel);
ExcelExportEntity adoptExcel = new ExcelExportEntity("通過數(shù)量", "adoptNum" + i);
adoptExcel.setStatistics(true);
exportEntities.add(adoptExcel);
group_1.setList(exportEntities);
colList.add(group_1);
}
//文件數(shù)據(jù)
List<Map<String, Object>> list = new ArrayList<>();
List<StoreNewAddReportVO.DistributorStoreNewAddReportVO> disList = register.getStoreNewAddReportVO().getDistributorStoreNewAddReportVOList();
int size = disList.size();
for (int i = 0; i < size; i++) {
StoreNewAddReportVO.DistributorStoreNewAddReportVO dis = disList.get(i);
Map<String, Object> valMap = new HashMap<>();
valMap.put("distributorName", dis.getDistributorName());
valMap.put("dept", dis.getDept());
valMap.put("region", dis.getRegion());
valMap.put("province", dis.getProvince());
valMap.put("storeNum", dis.getStoreNum());
List<StoreNewAddReportVO.dayData> dayDataList = dis.getDayDataList();
Map<String, List<StoreNewAddReportVO.dayData>> collectMap = Maps.newHashMap();
if (CollectionUtils.isNotEmpty(dayDataList)) {
collectMap = dayDataList.stream().collect(Collectors.groupingBy(StoreNewAddReportVO.dayData::getDayStr));
}
List<Map<String, Object>> list_1 = new ArrayList<>();
Map<String, Object> valMap_1 = new HashMap<>();
for (int j = 1; j <= dayNum; j++) {
List<StoreNewAddReportVO.dayData> dayData = collectMap.get(String.valueOf(j));
int applyflag = 0;
int adoptflag = 0;
if (CollectionUtils.isNotEmpty(dayData)) {
applyflag = dayData.get(0).getApplyNum();
adoptflag = dayData.get(0).getAdoptNum();
}
valMap_1.put("applyNum" + j, applyflag);
valMap_1.put("adoptNum" + j, adoptflag);
}
list_1.add(valMap_1);
valMap.put("day", list_1);
list.add(valMap);
}
//導(dǎo)出
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("【" + request.getMonthStr() + "】門店注冊日明細(xì)數(shù)據(jù)", "數(shù)據(jù)"), colList, list);
Sheet sheet = workbook.getSheet("數(shù)據(jù)");
Row row = sheet.getRow(sheet.getLastRowNum());
Cell cell = row.getCell(0);
cell.setCellValue("總計");
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 15);
font.setFontName("Trebuchet MS");
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
CellRangeAddress range_0 = new CellRangeAddress(sheet.getLastRowNum(), sheet.getLastRowNum(), 0, 3);
sheet.addMergedRegion(range_0);
File file = new File("D:\\".concat(UUID.randomUUID().toString().concat(".xls")));
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
workbook.write(fileOutputStream);
} catch (Exception e) {
log.error("門店注冊日workbook寫入到文件中失敗,錯誤信息:{}", ExceptionUtils.getStackTrace(e));
} finally {
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
//skip
}
}
}
具體的API細(xì)節(jié)就不介紹了可以去官網(wǎng),關(guān)鍵在于ExcelExportEntity 這個類,它是以map形式展現(xiàn)的,創(chuàng)建的時候設(shè)置key,設(shè)置value的根據(jù)key進(jìn)行設(shè)置,上面一些StoreNewAddReportVO還有其他是我的業(yè)務(wù)類, 到時候可以替換掉。
到此這篇關(guān)于Java中EasyPoi導(dǎo)出復(fù)雜合并單元格的方法的文章就介紹到這了,更多相關(guān)Java EasyPoi導(dǎo)出單元格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis 動態(tài)SQL之where標(biāo)簽的使用
本文主要介紹了MyBatis 動態(tài)SQL之where標(biāo)簽,where 標(biāo)簽主要用來簡化 SQL 語句中的條件判斷,可以自動處理 AND/OR 條件,下面就來具體介紹一下2024-01-01
Java使用DateUtils對日期進(jìn)行數(shù)學(xué)運(yùn)算經(jīng)典應(yīng)用示例【附DateUtils相關(guān)包文件下載】
這篇文章主要介紹了Java使用DateUtils對日期進(jìn)行數(shù)學(xué)運(yùn)算的方法,可實現(xiàn)針對日期時間的各種常見運(yùn)算功能,并附帶DateUtils的相關(guān)包文件供讀者下載使用,需要的朋友可以參考下2017-11-11
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(4)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
JAVA多線程Thread和Runnable的實現(xiàn)
java中實現(xiàn)多線程有兩種方法:一種是繼承Thread類,另一種是實現(xiàn)Runnable接口。2013-03-03

