美文网首页
定制化导出Excel

定制化导出Excel

作者: 自由主义者 | 来源:发表于2019-11-22 14:26 被阅读0次
 public void distributeDetailExcel(@RequestParam("id") String id, HttpServletResponse response) {
        Distribute distribute = distributeService.findByDistributeIdAndStatus(id);
        List<SaleUserRewardVo> saleUserRewardList = distributeService.findAllSaleUserReward(id);
        List list = new ArrayList();
        int i = 0;
        for(SaleUserRewardVo surv : saleUserRewardList ){
            Map<String, Object> map = new HashMap<String, Object>();
            i++;
            map.put("userId", i);
            map.put("userName", surv.getDistributeObjectName());
            map.put("userMoney", surv.getDistributeMoney());
            list.add(map);
        }

        TemplateExportParams params = new TemplateExportParams("doc/excelTemplate.xlsx", true);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("rewardKindStr", distribute.getRewardKindStr());
        data.put("authUserName", distribute.getAuthUserName());
        data.put("rewardSum", distribute.getRewardSum());
        data.put("rewardDate", distribute.getRewardDate());
        data.put("constrctNo", distribute.getConstrctNo());
        data.put("projectName", distribute.getProjectName());
        data.put("saleDept", distribute.getSaleDept());
        if("2".equals(distribute.getStatus())){
            distribute.setStatus("公示中");
        }
        if("3".equals(distribute.getStatus())){
            distribute.setStatus("已分配");
        }
        if("4".equals(distribute.getStatus())){
            distribute.setStatus("沟通中");
        }
        data.put("status", distribute.getStatus());
        data.put("list", list);
        Workbook workbook = ExcelExportUtil.exportExcel(params, data);
        try {
            ServletOutputStream out = response.getOutputStream();
            response.setCharacterEncoding("utf-8");
            String fileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".xlsx";
            response.setHeader("Content-disposition", "attachment;filename='" + fileName+"'");
            response.setContentType("application/octet-stream; charset=UTF-8");
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//需要导入的依赖:
 <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>1.1.2-beta4</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.apache.poi</groupId>
          <artifactId>poi-ooxml</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <!--<dependency>
      <groupId>org.redisson</groupId>
      <artifactId>redisson</artifactId>
      <version>3.9.1</version>
    </dependency>-->

    <!-- 导入导出EXCEL SDK -->
    <dependency>
      <groupId>cn.afterturn</groupId>
      <artifactId>easypoi-spring-boot-starter</artifactId>
      <version>4.1.2</version>
    </dependency>

Excel模板如下:


image.png

相关文章

网友评论

      本文标题:定制化导出Excel

      本文链接:https://www.haomeiwen.com/subject/ogtqwctx.html