Java中EasyPoi导出复杂合并单元格的⽅法
前⾔:
上星期做了⼀个Excel的单元格合并,⽤的是EasyPoi,我之前合并单元格都是原⽣的,第⼀次使⽤EasyPoi合并也不太熟悉,看着⽹上⾃⼰套⽤,使⽤后发现⽐原⽣的⽅便些,贡献⼀下,也给其他⽤到合并⽽且⽤的是EasyPoi的⼩伙伴节省下时间。
导出模板:
坐标:
版本号,⾃⼰来定,可以去官⽹查看:
<!-- easypoi 导⼊包 -->
<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>
实现代码:
/
/表头设置
List<ExcelExportEntity> colList = new ArrayList<ExcelExportEntity>();
ExcelExportEntity colEntity = new ExcelExportEntity("经销商", "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("门店数量", "storeNum");
colEntity.setNeedMerge(true);
colEntity.setStatistics(true);
colList.add(colEntity);
Map<String, Integer> map = MonthStr());
Integer dayNum = ("dayNum");
for (int i = 1; i <= dayNum; i++) {
ExcelExportEntity group_1 = new ExcelExportEntity(i + "⽇", "day");
List<ExcelExportEntity> exportEntities = new ArrayList<>();
ExcelExportEntity appalyExcel = new ExcelExportEntity("申请数量", "applyNum" + i);
appalyExcel.setStatistics(true);
exportEntities.add(appalyExcel);
ExcelExportEntity adoptExcel = new ExcelExportEntity("通过数量", "adoptNum" + i);
adoptExcel.setStatistics(true);
exportEntities.add(adoptExcel);
java64位
group_1.setList(exportEntities);
colList.add(group_1);
}
/
/⽂件数据
List<Map<String, Object>> list = new ArrayList<>();
List<StoreNewAddReportVO.DistributorStoreNewAddReportVO> disList = StoreNewAddReportVO().getDistributorStoreNewAddReportVOList();
int size = disList.size();
for (int i = 0; i < size; i++) {
StoreNewAddReportVO.DistributorStoreNewAddReportVO dis = (i);
Map<String, Object> valMap = new HashMap<>();
valMap.put("distributorName", DistributorName());
valMap.put("dept", Dept());
valMap.put("region", Region());
valMap.put("province", Province());
valMap.put("storeNum", StoreNum());
List<StoreNewAddReportVO.dayData> dayDataList = DayDataList();
Map<String, List<StoreNewAddReportVO.dayData>> collectMap = wHashMap();
if (CollectionUtils.isNotEmpty(dayDataList)) {
collectMap = dayDataList.stream().upingBy(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 = (String.valueOf(j));
int applyflag = 0;
int adoptflag = 0;
if (CollectionUtils.isNotEmpty(dayData)) {
applyflag = (0).getApplyNum();
adoptflag = (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);
}
//导出
Workbook workbook = portExcel(new ExportParams("【" + MonthStr() + "】门店注册⽇明细数据", "数据"), colList, list);
Sheet sheet = Sheet("数据");
Row row = LastRowNum());
Cell cell = Cell(0);
cell.setCellValue("总计");
CellStyle cellStyle = ateCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//⽔平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
Font font = ateFont();
font.setFontHeightInPoints((short) 15);
font.setFontName("Trebuchet MS");
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
CellRangeAddress range_0 = new LastRowNum(), LastRowNum(), 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) {
<("门店注册⽇workbook写⼊到⽂件中失败,错误信息:{}", StackTrace(e));
} finally {
if (null != fileOutputStream) {
try {
fileOutputStream.close();
} catch (IOException e) {
//skip
}
}
}
具体的API细节就不介绍了可以去官⽹,关键在于ExcelExportEntity 这个类,它是以map形式展现的,创建的时候设置key,设置value的根据key进⾏设置,上⾯⼀些StoreNewAddReportVO还有其他是我的业务类,到时候可以替换掉。
到此这篇关于Java中EasyPoi导出复杂合并单元格的⽅法的⽂章就介绍到这了,更多相关Java EasyPoi导出单元格内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。