实现Excel动态列数据的导出
in 工作心得 with 23 comments

实现Excel动态列数据的导出

in 工作心得 with 23 comments

前言: 本文旨在记录项目中多次出现的需求,实现Excel动态列及对应数据的导出。为了方便处理类似问题,在此记录

 <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>
        DynamicDataVO dynamicDataVO = robotWholeRateRecordService.getList(queryVO);
        //构造ExcelExportEntity list
        List<ExcelExportEntity> beanList = new ArrayList<ExcelExportEntity>();
        for (Map<String, Object> map : dynamicDataVO.getTableColumn()) {
            //构造对象等同于@Excel
            beanList.add(new ExcelExportEntity(map.get("label").toString(), map.get("property").toString()));
        }
        String fileName = "测试生成动态列.xls";
        Sting excelUrl = ExcelUtils.exportDynamicRow(new ExportParams("这是标题名称","这是sheet名称"), beanList, dynamicDataVO.getTableData(),fileName)
/**
     * 生成动态列Excel地址并上传至minio
     * @param exportParams
     * @param beanList
     * @param dataSet
     * @param fileName
     * @return
     * @throws Exception
     */
    public static String exportDynamicRow(ExportParams exportParams, List<ExcelExportEntity> beanList, Collection<?> dataSet, String fileName) throws Exception {

        File file = new File(ReviewConfig.getProfile()+fileName);
        OutputStream outputStream = null;
        Workbook workbook = null;
        try {
            outputStream= new FileOutputStream(file);
            workbook = ExcelExportUtil.exportExcel(exportParams, beanList, dataSet);
            workbook.write(outputStream);
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
        return MinioUtils.upload2MinIO(new File(ReviewConfig.getProfile()+fileName));
 public static String upload2MinIO(File file) throws Exception {
        //本地生成成功后 上传文件至minion
        MinioUtils.putObject(SpringUtils.getBean(MinioConfig.class).getBucketName(),file.getName(),new FileInputStream(file));
        return SpringUtils.getBean(MinioConfig.class).getEndpoint()+"/"+SpringUtils.getBean(MinioConfig.class).getBucketName()+"/"+file.getName();
    }
Responses / Cancel Reply
  1. 1

    1

    Reply
    1. 1
      @1

      1

      Reply
    2. 1
      @1

      1

      Reply
    3. 1
      @1

      1

      Reply
    4. lxbfYeaa
      @1

      555

      Reply
  2. 1

    1

    Reply
    1. 1
      @1

      1

      Reply
    2. 1
      @1

      1

      Reply
    3. 1
      @1

      1

      Reply
    4. lxbfYeaa
      @1

      1

      Reply
    5. lxbfYeaa
      @1

      1

      Reply
    6. lxbfYeaa
      @1

      555

      Reply
  3. 1

    1

    Reply
    1. 1
      @1

      1

      Reply
    2. 1
      @1

      1

      Reply
    3. 1
      @1

      1

      Reply
    4. 1
      @1

      1

      Reply
    5. 1
      @1

      1

      Reply
    6. lxbfYeaa
      @1

      555

      Reply
    7. lxbfYeaa
      @1

      1

      Reply
    8. lxbfYeaa
      @1

      1

      Reply
    9. lxbfYeaa
      @1

      555

      Reply
  4. 1

    1

    Reply