easypoi模版多sheet页导出、是真的easy

一.pom依赖导入
<!-- easypoi -->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.4.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml-schemas</artifactId>
   <version>4.1.1</version>
</dependency>

二.excel模版如下:

三.业务代码如下:
        //获取导出excel指定模版
        TemplateExportParams params = new TemplateExportParams("template/YHYQ.xlsx",true);

        //获取详情数据  (数据结构切套)
        PlYhYqFEntityDto plYhYqDtos = (PlYhYqFEntityDto) plYhYqService.selectPlYhYq(fid).getData();

        Map<Integer, List<Map<String, Object>>> sheetsMap = new HashMap<>();

        //以下无非就是做了一件事、插入数据
        try {
            //获取押汇批次下车辆
            List<YhPcFEntityDto> yhPcList = plYhYqDtos.getFEntity();
            int i = 0;
            // 将所有sheet使用得map进行包装
            for (YhPcFEntityDto yhPcFEntityDto : yhPcList) {
                List<Map<String, Object>> list =new ArrayList<>();
                Map<String, Object> map = new HashMap<>();
                //添加批次数据
                //信用证号
                map.put("xyzh", yhPcFEntityDto.getFCreditNo());
                //经销商名称
                map.put("FNAME",plYhYqDtos.getFCustomer());
                //进口合同号
                map.put("FPurchaseNo",yhPcFEntityDto.getFPurchaseNo());
                //上次到期日
                String dqr = yhPcFEntityDto.getFLastDueDate();
                //垫税开始日期
                String startDate = yhPcFEntityDto.getFBpDate();
                //上次到期日 - 垫税开始日期  =  押汇天数
                int dayBetweenTwoDate = DateUtil.getDayBetweenTwoDate(dqr, startDate) + 1;
                map.put("yhts", dayBetweenTwoDate);
                //延期期限
                map.put("qx", yhPcFEntityDto.getFDelayDays());
                //延期计划开始日期
                map.put("startjhDate", DateUtil.stringDate(yhPcFEntityDto.getFDelayStateDate(),DateUtil.YYYY_MM_DD));
                //延期结束日期
                map.put("endDate",  DateUtil.stringDate(yhPcFEntityDto.getFDelayEndDate(),DateUtil.YYYY_MM_DD));
                //延期年化利率
                BigDecimal fYearRate =bigDecimalXsw(new BigDecimal(yhPcFEntityDto.getFYearRate()),2);
                map.put("nll", fYearRate);
                //服务费价格标准
                String fPriceStandard = bigDecimalXsw(new BigDecimal(yhPcFEntityDto.getFPriceStandard()), 2).toPlainString();
                //押汇利息 = 延期年利率(%) * 服务费价格标准
                map.put("yhlx", "1".equals(fPriceStandard) ? fYearRate + "%" :
                        "2".equals(fPriceStandard) ? fYearRate + "%×(" + "1-保证金比例)" :
                                fYearRate + "%×(" + "1-累计保证金比例)");
                //保证金汇率
                BigDecimal fExceptExchangeRate=new BigDecimal(yhPcFEntityDto.getFDelayMarginRate());

                //添加车辆信息
                List<FEntityVehDto> fEntityVeh = yhPcFEntityDto.getFEntityVeh();
                //多少辆车
                map.put("number", fEntityVeh.size());
                List<Map<String, Object>> listMap = new ArrayList<>();
                //押汇总金金额
                BigDecimal FPrice = BigDecimal.ZERO;
                //延期保证金总额
                BigDecimal fMarginAmountTotal = BigDecimal.ZERO;
                for (FEntityVehDto fEntityVehDto : fEntityVeh) {
                    Map<String, Object> lm = new HashMap<>();
                    //品牌型号(车系)
                    lm.put("cx", fEntityVehDto.getFSerialName());
                    //VIN(车架号)
                    lm.put("F_VIN", fEntityVehDto.getFVinName());
                    //延期本金(CAD)
                    lm.put("yqbj", bigDecimalXsw(new BigDecimal(fEntityVehDto.getFPrice()), 2));
                    //应收保证金-RMB
                    lm.put("ysbzj", bigDecimalXsw(new BigDecimal(fEntityVehDto.getFPlanMarginAmount()), 2));
                    //币种
                    lm.put("bz", fEntityVehDto.getFBpCurrencyName());
                    //押汇总金金额累加
                    FPrice = FPrice.add(new BigDecimal(fEntityVehDto.getFPrice()));
                    //延期保证金累加金额
                    fMarginAmountTotal = fMarginAmountTotal.add(new BigDecimal(fEntityVehDto.getFPlanMarginAmount()));
        
                    listMap.add(lm);
                }

                //押汇金额 = 车辆的本金之和
                map.put("yhje", bigDecimalXsw(FPrice, 2));

                map.put("mapList",listMap);
                list.add(map);
                sheetsMap.put(i++,list);

            }
            
            //导出excel
            Workbook workbook = ExcelExportUtil.exportExcelClone(sheetsMap, params);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            workbook.write(baos);
            byte[] documentContent = baos.toByteArray();
            response.setContentType("application/octet-stream;charset=UTF-8");

            // 指定下载的文件名--设置响应头
            response.setHeader("Content-disposition", "attachment;filename="
                    + URLEncoder.encode("批量押汇延期申请.xlsx", "UTF-8"));
            // response响应头中设置传输文件长度
            response.setContentLength(documentContent.length);
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            bos.write(documentContent);
            workbook.close();
            baos.close();
            bos.close();
            buyerMap.remove(fid);
        } catch (IOException e) {
            log.error("批量押汇延期申请下载下载失败--->" + e.getMessage());
        }

以上是比较完整的实现代码

四.效果如下、多sheet页就完成了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值