SpringBoot-EasyExcel导出数据(带图片)

前言

EasyExcel 是阿里巴巴开源的一个Java操作Excel的技术,和EasyPoi一样是封装Poi的工具类。

但是不同的地方在于,在EasyExcel中解决了Poi技术读取大批量数据耗费内存的问题。

当然了,也封装了很多常用的Excel操作

  • 最基本的导入导出
  • 图片的导入导出
  • 大批量数据的导入导出
  • 模板的导出
  • 官方地址:https://alibaba-easyexcel.github.io/quickstart/write.html

一、引入的maven依赖

 <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.1.1</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

二、导出的实体类

EasyExcel也是注解式开发,常用注解如下

  • ExcelProperty 指定当前字段对应excel中的那一列
  • ExcelIgnore 默认所有字段都会和excel去匹配,加了这个注解会忽略该字段
  • DateTimeFormat 日期转换,用String去接收excel日期格式的数据会调用这个注解。里面的value参照java.text.SimpleDateFormat
  • NumberFormat 数字转换,用String去接收excel数字格式的数据会调用这个注解。里面的value参照java.text.DecimalFormat
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.songwp.config.SxjgUrlImageConverter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.net.URL;

/**
 * @Description 老师信息实体类
 * @Author songwp
 * @Date 2023/3/30 13:57
 **/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ContentRowHeight(120)
@AllArgsConstructor
@NoArgsConstructor
public class Teacher implements Serializable {

    /**
     * 教师编号
     */
    @ExcelProperty("教师编号")
    @ExcelIgnore
    private Integer teacherId;
    /**
     * 教师名称
     */
    @ExcelProperty("教师名称")
    private String teacherName;
    /**
     * 教师图片
     */
    @ExcelProperty(value = {"教师图片"},converter = SxjgUrlImageConverter.class)
    private URL teacherImage;
    /**
     * 教师状态: 0 -任教中 1 - 为任教
     */
    @ExcelProperty("教师状态")
    @ExcelIgnore
    private Integer teacherStatus;

    /**
     * 教师状态: 0 -任教中 1 - 为任教
     */
    @ExcelProperty("教师状态")
    private String teacherStatusStr;
    /**
     * 住址信息
     */
    @ExcelProperty("住址信息")
    private String address;

三、图片转换处理类

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 SxjgUrlImageConverter implements Converter<URL> {
    public static int urlConnectTimeout = 2000;
    public static int urlReadTimeout = 6000;

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

    @Override
    public WriteCellData<?> convertToExcelData(URL value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) throws IOException {
        InputStream inputStream = null;
        try {
            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();
            }
        }
    }
}

四、导出的工具类

import com.alibaba.excel.EasyExcel;
import com.sxjgkg.sjsk.innovation.InnovationConstants;
import com.sxjgkg.sjsk.zhaixing.core.ZhaixingException;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 *
 * @author songwp
 * @since 2023-04-20 14:04:42
 */
@Slf4j
public class EasyExcelUtil {

    /**
     * 导出excel
     *
     * @param response          响应实体类
     * @param sheetName         sheet页签名称
     * @param clazz             模板的字节码对象
     * @param list              需要导出的数据列表
     * @throws ZhaixingException 业务异常
     */
    public static void exportExcel(HttpServletResponse response,
                                   String sheetName,
                                   Class clazz,
                                   List<?> list) throws ZhaixingException {
        try {
            String fileName = URLEncoder.encode(sheetName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), clazz).autoCloseStream(Boolean.TRUE).sheet("sheet")
                    .doWrite(list);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ZhaixingException(InnovationConstants.Ex.EXPORT_EXCEPTION,e.getMessage());
        }
    }
}

五、导出方法测试

  /**
     *  教师信息表导出
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws MalformedURLException {
       // 准备数据
        List<Teacher> teachers = new ArrayList<>();
        teachers.add(new Teacher(1,"马云",new URL("https://money.gucheng.com/UploadFiles_6503/201901/2019012516201400.jpg"),1,"未任教","浙江杭州"));
        teachers.add(new Teacher(2,"王健林",new URL("https://img1.cache.netease.com/ent/2016/8/23/20160823221523324f5.jpg"),1,"未任教","上海浦东新区"));
        teachers.add(new Teacher(3,"雷军",new URL("https://i1.073img.com/140526/5808312_102252_1.jpg"),1,"未任教","北京通州区"));
        teachers.add(new Teacher(4,"马化腾",new URL("https://x0.ifengimg.com/res/2020/F43C0869EE9D07C77C0D82D13266BB1F94DBD148_size583_w1944_h1639.jpeg"),1,"未任教","中国深圳"));
     // 方法一        
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = null;
        try {
            fileName = URLEncoder.encode("教师信息表", "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        try {
            EasyExcel.write(response.getOutputStream(), Teacher.class).sheet("教师信息表").doWrite(teachers);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
     // 方法二
     EasyExcelUtil.exportExcel(response,"教师信息表", Teacher.class,teachers) 
    }

六、导出文件效果展示

 

posted @ 2023-03-30 11:22  [奋斗]  阅读(5859)  评论(0编辑  收藏  举报