[alibaba/easyexcel]导出图片多,如何提高导出速度

2024-05-16 668 views
8

异常代码

   /**
 * 作业人员信息表导出-vo
 *
 * @author linsm
 * @date 2022-03-24 11:11
 */
@Data
@ContentRowHeight(100)
public class WorkerExportVO {

    /**
     * 工程名称
     */
    @ExcelProperty(value = "工程名称")
    @ColumnWidth(20)
    private String projectName;

    /**
     * 姓名
     */
    @ExcelProperty(value = "姓名")
    private String userName;

    /**
     * 性别
     */
    @ExcelProperty(value = "性别")
    private String sex;

    /**
     * 身份证号码
     */
    @ExcelProperty(value = "身份证号码")
    @ColumnWidth(25)
    private String idCardNo;

    /**
     * 家庭地址
     */
    @ExcelProperty(value = "家庭地址")
    @ColumnWidth(30)
    private String homeAddress;

    /**
     * 年龄
     */
    @ExcelProperty(value = "年龄")
    private Integer age;

    /**
     * 联系电话
     */
    @ExcelProperty(value = "联系电话")
    @ColumnWidth(20)
    private String phone;

    /**
     * 包工头姓名
     */
    @ExcelProperty(value = "包工头姓名")
    private String contractorName;

    /**
     * 工种
     */
    @ExcelProperty(value = "工种")
    private String workGroup;

    /**
     * 14天内是否接触过中高风险地区人员(0-否,1-是)
     */
    @ExcelProperty(value = "14天内是否接触过中高风险地区人员")
    private String isTouch;

    /**
     * 14天内是否到过中高风险地区(0-否,1-是)
     */
    @ExcelProperty(value = "14天内是否到过中高风险地区")
    private String isArrive;

    /**
     * 从哪个地区到本市(1-本市,2-本省其他市,3-外省其他市)
     */
    @ExcelProperty(value = "从哪个地区到本市")
    private String fromCity;

    /**
     * 健康码颜色(1-绿,2-黄,3-蓝)
     */
    @ExcelProperty(value = "健康码颜色")
    private String healthCodeColor;

    /**
     * 新冠疫苗接种次数
     */
    @ExcelProperty(value = "新冠疫苗接种次数")
    private Integer vaccinationStatus;

    /**
     * 入场时间
     */
    @ColumnWidth(20)
    @ExcelProperty(value = "入场时间")
    @DateTimeFormat("yyyy-MM-dd")
    private Date entryTime;

    /**
     * 退场时间
     */
    @ColumnWidth(20)
    @ExcelProperty(value = "退场时间")
    @DateTimeFormat("yyyy-MM-dd")
    private Date exitTime;

    @ColumnWidth(20)
    @ExcelProperty(value = "更新时间")
    @DateTimeFormat("yyyy-MM-dd")
    private Date updateTime;

    /**
     * 身份证正面url
     */
    @ExcelProperty(value = "身份证正面")
    @ColumnWidth(20)
    private URL idCardFront;

    /**
     * 身份证反面url
     */
    @ExcelProperty(value = "身份证反面")
    @ColumnWidth(20)
    private URL idCardBack;

    /**
     * 粤康码url
     */
    @ExcelProperty(value = "粤康码")
    @ColumnWidth(20)
    private URL healthCode;

    /**
     * 行程卡url
     */
    @ExcelProperty(value = "行程卡")
    @ColumnWidth(20)
    private URL itineraryCard;

    /**
     * 核酸报告url
     */
    @ExcelProperty(value = "核酸报告")
    @ColumnWidth(20)
    private URL nucleicAcidReport;

    /**
     * 空白图片1
     */
    @ExcelProperty(value = "空白图片1")
    @ColumnWidth(20)
    private URL blankUrl1;

    /**
     * 空白图片2
     */
    @ExcelProperty(value = "空白图片2")
    @ColumnWidth(20)
    private URL blankUrl2;

    /**
     * 空白图片3
     */
    @ExcelProperty(value = "空白图片3")
    @ColumnWidth(20)
    private URL blankUrl3;
}

  private URL getUrl(String url) throws MalformedURLException {
        return url == null ? null : new URL(url);
    }

    public void export(Integer projectId, HttpServletResponse response) throws Exception {
        List<Worker> list = this.lambdaQuery().eq(Worker::getProjectId, projectId).list();
        if(CollectionUtils.isEmpty(list)) {
            throw new ServiceException("导出数据为空");
        }
        List<WorkerExportVO> exportList = Lists.newArrayList();
        for(Worker worker : list) {
            WorkerExportVO workerExportVO = new WorkerExportVO();
            BeanUtil.copyProperties(worker,workerExportVO);
            //赋值
            workerExportVO.setSex(worker.getSex() == 1 ? "男" : "女");
            workerExportVO.setWorkGroup(WorkGroup.getByCode(worker.getWorkGroup()));
            if(worker.getFromCity() != null) {
                workerExportVO.setFromCity(worker.getFromCity() == 1 ? "本市" : worker.getFromCity() == 2 ? "本省其他市" : "外省其他市");
            }
            if(worker.getHealthCodeColor() != null) {
                workerExportVO.setHealthCodeColor(worker.getHealthCodeColor() == 1 ? "绿" : worker.getHealthCodeColor() == 2 ? "黄" : "蓝");
            }
            if(worker.getIsArrive() != null) {
                workerExportVO.setIsArrive(worker.getIsArrive() == 1 ? "是" : "否");
            }
            if(worker.getIsTouch() != null) {
                workerExportVO.setIsTouch(worker.getIsTouch() == 1 ? "是" : "否");
            }
            workerExportVO.setHealthCode(getUrl(worker.getHealthCode()));
            workerExportVO.setIdCardBack(getUrl(worker.getIdCardBack()));
            workerExportVO.setIdCardFront(getUrl(worker.getIdCardFront()));
            workerExportVO.setItineraryCard(getUrl(worker.getItineraryCard()));
            workerExportVO.setNucleicAcidReport(getUrl(worker.getNucleicAcidReport()));
            workerExportVO.setBlankUrl1(getUrl(worker.getBlankUrl1()));
            workerExportVO.setBlankUrl2(getUrl(worker.getBlankUrl2()));
            workerExportVO.setBlankUrl3(getUrl(worker.getBlankUrl3()));
            exportList.add(workerExportVO);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        String fileName = URLEncoder.encode("作业人员信息导出", StandardCharsets.UTF_8.name());
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WorkerExportVO.class).sheet("导出数据").doWrite(exportList);
    }

异常提示

目前导出4条记录,导出的excel大小有2m,需要6s多,好像大部分时间都耗费在io上面

建议描述

回答

8

我也是同样的问题,四千多条数据 需要十分钟,全部在io上耗时了,有没有可能把图片先多线程下载到本地,然后在本地处理处理?

5

可以考虑 压缩图片之类的方案