diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcResiUserController.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcResiUserController.java index de5d3c2a05..753b3e5fe4 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcResiUserController.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/controller/IcResiUserController.java @@ -17,11 +17,11 @@ package com.epmet.controller; -import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity; import cn.afterturn.easypoi.excel.export.ExcelExportService; import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; @@ -43,9 +43,9 @@ import com.epmet.dto.result.FormItem; import com.epmet.excel.IcResiUserExcel; import com.epmet.feign.OperCustomizeOpenFeignClient; import com.epmet.service.IcResiUserService; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.Name; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -144,29 +144,21 @@ public class IcResiUserController { } System.out.println(JSON.toJSONString(resultForm.getData())); - + XSSFWorkbook workbook = new XSSFWorkbook(); Map> sheetHeaderMap = buildHeaderByItem(resultForm); - Workbook workbook = null; + //Workbook workbook = null; for (Map.Entry> entry : sheetHeaderMap.entrySet()) { String sheetName = entry.getKey(); List headers = entry.getValue(); - ExportParams exportParams = new ExportParams(null, sheetName); - - if (workbook == null) { - List> dataList =new ArrayList<>(); - Map dataMap = new HashMap<>(); - dataMap.put("GENDER0", "1"); - dataMap.put("ID_CARD0", "371888991"); - dataList.add(dataMap); - workbook = ExcelExportUtil.exportExcel(exportParams, headers, dataList); - } + ExportParams exportParams = new ExportParams(); + exportParams.setSheetName(sheetName); + exportParams.setAutoSize(true); new ExcelExportService().createSheetForMap(workbook, exportParams, headers,new ArrayList<>()); } + List allNames = workbook.getAllNames(); + System.out.println("======"+allNames); - //exportParams.setDictHandler(new ExcelDiceAddressListHandlerImpl()); - System.out.println("header:"+JSON.toJSONString(sheetHeaderMap)); - //Workbook workbook = ExcelExportUtil.exportExcel( new ExportParams(), sheetHeaderMap.get(), dataList); FileOutputStream fos = new FileOutputStream("//Users/liujianjun/Downloads/基础信息表/Dow.tt.xls"); workbook.write(fos); fos.close(); @@ -174,32 +166,61 @@ public class IcResiUserController { @NotNull private Map> buildHeaderByItem(Result resultForm) { - + //form中的itemlist 为一级表头 但是要排除每个item中含有childGroup的 List itemList = resultForm.getData().getItemList(); List groupList = resultForm.getData().getGroupList(); Map> everySheetHeaderMap = new LinkedHashMap<>(); + List firstSheetHeaderList = new ArrayList<>(); itemList.forEach(item->{ if (StringUtils.isBlank(item.getColumnName())){ return; } - ExcelExportEntity header = new ExcelExportEntity(item.getLabel(),item.getColumnName().concat(String.valueOf(item.getColumnNum()))); - header.setNeedMerge(true); + ExcelExportEntity header = new ExcelExportEntity(item.getLabel(),item.getColumnName().concat(String.valueOf(item.getColumnNum())),30); + header.setNeedMerge(true); + if (item.getChildGroup() == null){ + System.out.println(item.getLabel()); + firstSheetHeaderList.add(header); + return; + } + everySheetHeaderMap.putIfAbsent(resultForm.getData().getFormName(),firstSheetHeaderList); //这些是动态的 formGroup if (item.getChildGroup() != null){ //baseTableName单独的一个sheet - System.out.println(item.getLabel()); + System.out.println("childGroup:"+item.getLabel()); if (BASE_TABLE_NAME.equals(item.getTableName())){ header = new ExcelExportEntity(item.getChildGroup().getLabel(),item.getChildGroup().getTableName()); header.setNeedMerge(true); - buildHeader(everySheetHeaderMap, item, header); + List otherSheetHeaderList = new ArrayList<>(); + List secondHeaderList = new ArrayList<>(); + //这里是设置除基础信息之外的sheet的表头 + item.getChildGroup().getItemList().forEach(item2->{ + if (!BASE_TABLE_NAME.equals(item2.getTableName())){ + + everySheetHeaderMap.putIfAbsent(item.getLabel(),otherSheetHeaderList); + } + ExcelExportEntity secondHeader = new ExcelExportEntity(item2.getLabel(),item2.getColumnName().concat(String.valueOf(item2.getColumnNum()))); + secondHeader.setNeedMerge(true); + secondHeaderList.add(secondHeader); + if (!item2.getItemType().equals("radio") && com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(item2.getOptions())){ + List thirdHeaderList = new ArrayList<>(); + item2.getOptions().forEach(child->{ + ExcelExportEntity thirdHeader = new ExcelExportEntity(child.getLabel()); + thirdHeaderList.add(thirdHeader); + }); + secondHeader.setList(thirdHeaderList); + } + + }); + header.setList(secondHeaderList); + otherSheetHeaderList.add(header); } } }); - List firstSheetHeaderList = new ArrayList<>(); + //List firstSheetHeaderList = new ArrayList<>(); groupList.forEach(item->{ /* if (!"兴趣爱好".equals(item.getLabel()) && !"宗教信仰".equals(item.getLabel())){ return; @@ -212,13 +233,13 @@ public class IcResiUserController { header.setNeedMerge(true); List secondHeaderList = new ArrayList<>(); item.getItemList().forEach(item2->{ - ExcelExportEntity secondHeader = new ExcelExportEntity(item2.getLabel(),item2.getColumnName().concat(String.valueOf(item2.getColumnNum())),30); + ExcelExportEntity secondHeader = new ExcelExportEntity(item2.getLabel(),item2.getColumnName().concat(String.valueOf(item2.getColumnNum()))); secondHeader.setNeedMerge(true); secondHeaderList.add(secondHeader); - if (CollectionUtils.isNotEmpty(item2.getOptions())){ + if (com.baomidou.mybatisplus.core.toolkit.CollectionUtils.isNotEmpty(item2.getOptions())){ List thirdHeaderList = new ArrayList<>(); item2.getOptions().forEach(child->{ - ExcelExportEntity thirdHeader = new ExcelExportEntity(child.getLabel(),child.getValue()+ new Random(1).nextInt(2),30); + ExcelExportEntity thirdHeader = new ExcelExportEntity(child.getLabel(),child.getValue()+ new Random(1).nextInt(2)); thirdHeader.setNeedMerge(true); thirdHeaderList.add(thirdHeader); }); @@ -234,7 +255,6 @@ public class IcResiUserController { } private void buildHeader(Map> everySheetHeaderMap, FormItem item, ExcelExportEntity header) { - List firstSheetHeaderList = new ArrayList<>(); List secondHeaderList = new ArrayList<>(); item.getChildGroup().getItemList().forEach(item2->{ @@ -256,6 +276,7 @@ public class IcResiUserController { }); header.setList(secondHeaderList); firstSheetHeaderList.add(header); + everySheetHeaderMap.putIfAbsent(item.getLabel(),firstSheetHeaderList); } }