EasyPoi模板导出(多sheet导出)

EasyPoi模板导出(多sheet导出)


前言

一、引入包

引入easypoi的相关包

二、代码实现

代码如下(示例):

@Override
    public void export(LaborCostSettlement laborCostSettlement, HttpServletRequest request, HttpServletResponse response) {
        Map<Integer, Map<String, Object>> map = Maps.newHashMap();
        // 定义导出内容 -- 主表数据
        Map<String,Object> paramMap = Maps.newHashMap();
        // 定义导出内容 -- 子表数据
        Map<String,Object> paramMapChildOne = Maps.newHashMap();
        Map<String,Object> paramMapChildTwo = Maps.newHashMap();
        Map<String,Object> paramMapChildThree = Maps.newHashMap();
        Map<String,Object> paramMapChildFour = Maps.newHashMap();
        Map<String,Object> paramMapChildFive = Maps.newHashMap();
        Map<String,Object> paramMapChildSix = Maps.newHashMap();
        Map<String,Object> paramMapChildSeven = Maps.newHashMap();
        // 设置主表数据 -- 工程结算书总数据
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("a.del_flag", CommonConstant.STATUS_0);
        if(StringUtils.isNotBlank(laborCostSettlement.getId())){
            queryWrapper.eq("a.id", laborCostSettlement.getId());
        }
        List<LaborCostSettlement> list = baseMapper.findDataList(queryWrapper);
        LaborCostSettlement settlement = new LaborCostSettlement();
        if(CollectionUtil.isNotEmpty(list)){
            settlement = list.get(0);
        }
        if(ObjectUtil.isNotEmpty(settlement.getCurrentSettleAmount())){
            settlement.setChineseAmount(Convert.digitToChinese(settlement.getCurrentSettleAmount()));
        }
        paramMap.put("settlement", settlement);
        paramMap.put("zero", BigDecimal.valueOf(0).setScale(2, BigDecimal.ROUND_HALF_UP));
        // 结算表数据
        paramMapChildOne.put("settlement", settlement);
        paramMapChildOne.put("zero", BigDecimal.valueOf(0).setScale(2, BigDecimal.ROUND_HALF_UP));
        // 工程费统计表-浇筑
        LaborCostSettlementChild child = new LaborCostSettlementChild();
        child.setZid(laborCostSettlement.getId());
        child.setCategory("1");
        List<LaborCostSettlementChild> childOne = childService.findChildData(child);
        dealWithChildList(childOne, paramMapChildTwo);
        // 人工费单价 总价
        paramMapChildTwo.put("artificialCostPrice", ObjectUtil.isNotEmpty(settlement.getArtificialCostPrice()) ? settlement.getArtificialCostPrice() : 0);
        paramMapChildTwo.put("artificialCostTotal", ObjectUtil.isNotEmpty(settlement.getArtificialCostTotal()) ? settlement.getArtificialCostTotal() : 0);
        // 人工费合计
        BigDecimal total = BigDecimal.valueOf(Double.parseDouble(
                ObjectUtil.isNotEmpty(paramMapChildTwo.get("total")) ? paramMapChildTwo.get("total").toString() : "0"
        ));
        paramMapChildTwo.put("personTotal", total.add(ObjectUtil.isNotEmpty(settlement.getArtificialCostTotal()) ? settlement.getArtificialCostTotal() : BigDecimal.ZERO));
        paramMapChildTwo.put("childList", childOne);
        paramMapChildTwo.put("attachTeamName", settlement.getAttachTeamName());
        paramMapChildTwo.put("partyBUnit", settlement.getPartyBUnit());
        paramMapChildTwo.put("zero", BigDecimal.valueOf(0).setScale(2, BigDecimal.ROUND_HALF_UP));
        // 工程费统计表-钢筋
        child.setCategory("2");
        List<LaborCostSettlementChild> childTwo = childService.findChildData(child);
        dealWithChildList(childTwo, paramMapChildThree);
        paramMapChildThree.put("childList", childTwo);
        paramMapChildThree.put("attachTeamName", settlement.getAttachTeamName());
        paramMapChildThree.put("partyBUnit", settlement.getPartyBUnit());
        // 工程费统计表-钢绞线
        child.setCategory("3");
        List<LaborCostSettlementChild> childThree = childService.findChildData(child);
        dealWithChildList(childThree, paramMapChildFour);
        paramMapChildFour.put("childList", childThree);
        paramMapChildFour.put("attachTeamName", settlement.getAttachTeamName());
        paramMapChildFour.put("partyBUnit", settlement.getPartyBUnit());
        // 工程量确认表-浇筑
        paramMapChildFive.put("childList", childOne);
        paramMapChildFive.put("attachTeamName", settlement.getAttachTeamName());
        paramMapChildFive.put("partyBUnit", settlement.getPartyBUnit());
        paramMapChildFive.put("strete", paramMapChildTwo.get("strete"));
        // 工程量确认表-钢筋
        paramMapChildSix.put("childList", childTwo);
        paramMapChildSix.put("attachTeamName", settlement.getAttachTeamName());
        paramMapChildSix.put("partyBUnit", settlement.getPartyBUnit());
        paramMapChildSix.put("strete", paramMapChildThree.get("strete"));
        // 工程量确认表-钢绞线
        paramMapChildSeven.put("childList", childThree);
        paramMapChildSeven.put("attachTeamName", settlement.getAttachTeamName());
        paramMapChildSeven.put("partyBUnit", settlement.getPartyBUnit());
        paramMapChildSeven.put("strete", paramMapChildFour.get("strete"));

        settingMapNull(paramMapChildTwo);
        settingMapNull(paramMapChildThree);
        settingMapNull(paramMapChildFour);
        settingMapNull(paramMapChildFive);
        settingMapNull(paramMapChildSix);
        settingMapNull(paramMapChildSeven);
        String fileName = "运输结算.xlsx";
        String templatePath = upLoadPath + File.separator + TEMPLE_URL + File.separator +  "person/exportPersonSettleMxOne.xlsx";
        File folder = new File(upLoadPath + File.separator + TEMPLE_URL + File.separator + "person");
        // 文件夹不存在则创建文件夹
        if(!folder.exists()){
            folder.mkdirs();
        }
        // 文件不存在 则联系管理员添加导出模板
        File file = new File(templatePath);
        if(!file.exists()){
            throw new JeecgBootException("导出模板不存在,请联系管理员在【" + templatePath + "】路径添加导出模板!");
        }
        try {
            map.put(0, paramMap);
            map.put(1, paramMapChildOne);
            map.put(2, paramMapChildTwo);
            map.put(3, paramMapChildThree);
            map.put(4, paramMapChildFour);
            map.put(5, paramMapChildFive);
            map.put(6, paramMapChildSix);
            map.put(7, paramMapChildSeven);
            TemplateExportParams exportParams = new TemplateExportParams(templatePath, map.keySet().toArray(new Integer[]{}));
            Workbook workbook = ExcelExportUtil.exportExcel(map, exportParams);
            // 强制计算
            workbook.setForceFormulaRecalculation(true);
            response.addHeader("filename", URLEncoder.encode(fileName,"utf-8"));
            response.addHeader("Access-Control-Expose-Headers","filename");
            response.setHeader("Content-disposition", "attachment; fileName=" + URLEncoder.encode(fileName, "utf-8"));
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    public void settingMapNull(Map<String,Object> paramMap){
        if(paramMap.get("childList") == null){
            ArrayList<String> emptyList = new ArrayList<>();
            emptyList.add("");
            paramMap.put("childList",emptyList);
        }
    }

    public void dealWithChildList(List<LaborCostSettlementChild> child, Map<String,Object> paramMap){
        BigDecimal strete = BigDecimal.ZERO;
        BigDecimal total = BigDecimal.ZERO;
        if(CollectionUtil.isNotEmpty(child)){
            int num = 1;
            for (LaborCostSettlementChild child1 : child){
                strete = strete.add(ObjectUtil.isNotEmpty(child1.getTotalQuantity()) ? child1.getTotalQuantity() : BigDecimal.ZERO);
                total = total.add(ObjectUtil.isNotEmpty(child1.getTotalAmount()) ? child1.getTotalAmount() : BigDecimal.ZERO);
                if(StringUtils.isNotBlank(child1.getMeteringUnit())){
                    child1.setMeteringUnit(baseAPI.translateDict("aux_material_unit", child1.getMeteringUnit()));
                }
                child1.setProductionProcessName(baseAPI.translateDict("production_process", child1.getProductionProcess()));
                child1.setNumIndex(num);
                num++;
            }
        }
        paramMap.put("strete", strete);
        paramMap.put("total", total);
    }

模板示例:
在这里插入图片描述
在这里插入图片描述
导出示例
在这里插入图片描述
在这里插入图片描述

三、总结

	多sheet采用
	 TemplateExportParams exportParams = new TemplateExportParams(templatePath, map.keySet().toArray(new Integer[]{}));
     Workbook workbook = ExcelExportUtil.exportExcel(map, exportParams);

     如果含有计算则使用workbook.setForceFormulaRecalculation(true);
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
easypoi是一个用于Excel和Word文档操作的Java库。它提供了简单易用的API,可以通过模板导出Excel文件。下面是使用easypoi进行模板导出的示例代码: 1. 导入easypoi的maven坐标: ```xml <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.2.0</version> </dependency> ``` 2. 在Spring Boot的配置文件(bootstrap.yml或application.yml)中配置模板的URL: ```yaml easypoi: template: 'http://www.xxx.cn/statics/template/port.xlsx' ``` 3. 使用easypoi进行模板导出: ```java import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.entity.TemplateExportParams; import org.apache.poi.ss.usermodel.Workbook; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; public class TemplateExportDemo { public static void main(String[] args) throws IOException { // 模板文件的URL String templateUrl = "http://www.xxx.cn/statics/template/port.xlsx"; // 下载模板文件 URL url = new URL(templateUrl); URLConnection connection = url.openConnection(); InputStream inputStream = connection.getInputStream(); // 加载模板文件 Workbook workbook = ExcelExportUtil.importExcel(inputStream); // 创建模板参数 TemplateExportParams params = new TemplateExportParams(); params.setSheetNum(0); // 指定导出Sheet页 // 创建数据模型 Map<String, Object> dataModel = new HashMap<>(); dataModel.put("name", "John"); dataModel.put("age", 25); // 导出Excel文件 FileOutputStream outputStream = new FileOutputStream("output.xlsx"); ExcelExportUtil.exportExcel(params, dataModel, workbook.getSheetAt(params.getSheetNum()), outputStream); // 关闭流 outputStream.close(); inputStream.close(); } } ``` 这段代码会从指定的URL下载模板文件,然后根据模板和数据模型生成新的Excel文件。你可以根据自己的需求修改模板文件和数据模型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值