EasyExcel实现导出图片到excel

news/2024/7/21 5:19:59 标签: excel, java, easyexcel

pom依赖:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.0</version>
</dependency>

实体类:

java">package com.aicut.monitor.vo;

import com.aicut.monitor.utils.UrlImageConverter;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 * 豁口图片视图类
 * @author zhangzhi
 */
@ExcelIgnoreUnannotated
@ContentRowHeight(120)
@Getter
@Setter
@ToString
public class CutterImageVO extends BaseVO{

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @ExcelIgnore
    @Schema(description="主键")
    private Long id;

    /**
     * 工厂编码
     */
    @Schema(description="工厂编码")
    @ExcelProperty(value = "工厂编码")
    private String factoryCode;

    /**
     * 产线编码
     */
    @Schema(description="产线编码")
    @ExcelProperty(value = "产线编码")
    private String productionLineCode;

    /**
     * 设备编号
     */
    @Schema(description="设备编号")
    @ExcelProperty(value = "设备编号")
    private String deviceNumber;

    /**
     * 设备名称
     */
    @Schema(description="设备名称")
    @ExcelProperty(value = "设备名称")
    private String deviceName;

    /**
     * 分切刀编号
     */
    @Schema(description="分切刀编号")
    @ExcelProperty(value = "分切刀编号")
    private String cutterCode;

    /**
     * 是否磨损
     */
    @Schema(description="是否磨损")
    @ExcelProperty(value = "是否磨损")
    private Integer wearOrNot;

    /**
     * 检测时间
     */
    @Schema(description="检测时间")
    @ExcelProperty(value = "检测时间")
    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(20)
    private Date detectionTime;

    /**
     * 图片路径
     */
    @Schema(description="图片路径")
    @ExcelProperty(value = "豁口图片",converter = UrlImageConverter.class)
    @ColumnWidth(20)
    private String imageUrl;

    /**
     * 建议操作
     */
    @Schema(description="建议操作")
    @ExcelProperty(value = "建议操作")
    private String remark;
}

导出excel部分代码:

java">        String fileName = "豁口图片数据.xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            EasyExcel.write(response.getOutputStream(), CutterImageVO.class)
                    .sheet("豁口图片数据")
                    .doWrite(cutterImageVOList);
        }catch (Exception e){
            log.error(e.getMessage());
        }

String类型图片转换器:

java">package com.aicut.monitor.utils;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.IoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Description 图片处理
 * @Author songwp
 * @Date 2023/3/30 15:04
 **/
@Slf4j
public class UrlImageConverter implements Converter<String> {
    public static int urlConnectTimeout = 2000;
    public static int urlReadTimeout = 6000;

    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(String url, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) throws IOException {
        InputStream inputStream = null;
        try {
            URL value = new URL(url);
            if (ObjectUtils.isEmpty(value)){
                return new WriteCellData<>("图片链接为空");
            }
            URLConnection urlConnection = value.openConnection();
            urlConnection.setConnectTimeout(urlConnectTimeout);
            urlConnection.setReadTimeout(urlReadTimeout);
            inputStream = urlConnection.getInputStream();
            byte[] bytes = IoUtils.toByteArray(inputStream);
            return new WriteCellData<>(bytes);
        }catch (Exception e){
            log.info("图片获取异常",e);
            return new WriteCellData<>("图片获取异常");
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }
}

导出效果:


http://www.niftyadmin.cn/n/5346138.html

相关文章

申万宏源基于 StarRocks 构建实时数仓

作者 &#xff1a;申万宏源证券 实时数仓项目组 小编导读&#xff1a; 申万宏源证券有限公司是由新中国第一家股份制证券公司——申银万国证券股份有限公司与国内资本市场第一家上市证券公司——宏源证券股份有限公司&#xff0c;于 2015 年 1 月 16 日合并组建而成&#xff0c…

微信小程序开发如何实现阴影/悬浮效果

显示&#xff1a; 实现&#xff1a; <view style"width: 100%;height: 500rpx; display: flex; justify-content:space-evenly;align-items: center; "><view style"width: 200rpx;height:100rpx;background-color: aqua; display: flex; align-item…

蓝桥杯练习题

题目&#xff1a;每—本正式出版的图书都有一个ISBN号码与之对应&#xff0c;ISBN码包括9位数字、1位识别码和3位分隔符&#xff0c;其规定格式如“x-xXX-XXXxx-x”&#xff0c;其中符号"-"是分隔符(键盘上的减号)&#xff0c;最后—位是识别码&#xff0c;例如O-670…

精要图示:园区金融数字化服务蓝图,以园区为支点推动信贷业务增长

作为企业集聚地&#xff0c;园区已然成为银行业夯实客群基础的重要切口&#xff0c;各大行陆续围绕园区场景创新金融产品&#xff0c;以期抢跑园区金融新赛道、把握新增量。 启信慧眼首推一站式【园区金融】数字化服务方案&#xff0c;该方案同时支持启信天元私有化部署&#x…

微软人工智能办公AI工具 Copilot Pro 11项 Copilot 功能

Copilot&#xff08;曾用名 Bing Chat 和 Bing Chat Enterprise&#xff09;在此期间成为了许多用户的日常AI伴侣&#xff0c;并在正式发布后将继续为用户提供AI驱动的网络聊天体验。 微软Copilot官方网址链接&#xff1a;Microsoft Copilot: 你的日常 AI 助手 Copilot详情&am…

coding推送代码Jenkins自动构建部署

实现功能&#xff1a;我们向coding推送代码&#xff0c;通过webhook自动通知Jenkins&#xff0c;实现自动构建部署 coding 项目设置 / 开发者选项 / Service Hook 输入以下参数 发送POST请求服务 URL&#xff1a;htttp://xxx用户名&#xff1a;xxx密码&#xff1a;xxx Jen…

大数据开发之Spark(完整版)

第 1 章&#xff1a;Spark概述 1.1 什么是spark 回顾&#xff1a;hadoop主要解决&#xff0c;海量数据的存储和海量数据的分析计算。 spark是一种基于内存的快速、通用、可扩展的大数据分析计算引擎。 1.2 hadoop与spark历史 hadoop的yarn框架比spark框架诞生的晚&#xff…

openssl3.2/test/certs - 061 - other@good.org not permitted by CA1

文章目录 openssl3.2/test/certs - 061 - othergood.org not permitted by CA1概述笔记END openssl3.2/test/certs - 061 - othergood.org not permitted by CA1 概述 openssl3.2 - 官方demo学习 - test - certs 笔记 /*! * \file D:\my_dev\my_local_git_prj\study\openSS…