EasyExcel使用方式(包含导出图片)

1、导EasyExcel依赖

XML 复制代码
<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>easyexcel</artifactId>
   <version>3.3.2</version>
</dependency>

2、创建导出excel的实体类

java 复制代码
@Getter
@Setter
@EqualsAndHashCode
@HeadStyle(fillForegroundColor = 9)
@HeadFontStyle(fontHeightInPoints = 12, bold = BooleanEnum.TRUE)       //头字体   加粗
@HeadRowHeight(31)
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER,borderTop = BorderStyleEnum.THIN,borderBottom = BorderStyleEnum.THIN,borderLeft = BorderStyleEnum.THIN,borderRight = BorderStyleEnum.THIN) //水平居中  垂直居中  上 下 左 右(边框线)
@ContentFontStyle(fontHeightInPoints = 11)    //内容字体
@ContentRowHeight(123)

public class ExcelDto {
        @ExcelProperty(value = "序号", index = 0)  //意思为序号这一列为第0列(从0开始算)
        @ColumnWidth(14)
        private String number;
        @ExcelProperty(value = "姓名", index = 1)
        @ColumnWidth(18)
        private String name;
        @ExcelProperty(value = "时间", index = 2)
        @ColumnWidth(35)
        private Date time;
        @ColumnWidth(54)
        @ExcelProperty(value = "备注", index = 3)
        private String text;
        @ExcelProperty(value = "图片", index = 4)
        @ContentStyle(shrinkToFit = BooleanEnum.TRUE,borderTop = BorderStyleEnum.THIN,borderBottom = BorderStyleEnum.THIN,borderLeft = BorderStyleEnum.THIN,borderRight = BorderStyleEnum.THIN)  //上下左右(边框线)
        @ColumnWidth(43)
        private File image;  //file类型
}

3、代码进行逻辑赋值

controller

java 复制代码
 /**
   * 导出excel
   */
    @GetMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response, HttpServletRequest request,T entity) {
        jsjtProblemLabelService.exportExcel(response, request,entity);
    }

service

java 复制代码
    /**
     * 导出
     * @param response
     * @param request
     */
    public void exportExcel(HttpServletResponse response, HttpServletRequest request,T entity) {
        try {

            //创建导出excel的集合
            List<ExcelDto> list = ListUtils.newArrayList();
            
            //假设excelListBySQL 为数据库中查询数据的集合
            List<Excel> excelListBySQL = new ArrayList();
            
            //处理逻辑代码取出符合条件的值返回进excelListBySQL中 

            //循环赋值
            for (int i = 0; i < excelListBySQL.size(); i++) {

                //取出每一个从数据库中取出的对象
                Excel excel = excelListBySQL.get(i);

                //返回进导出excel的类中
                ExcelDto excelDto = new ExcelDto();

                //赋值序号
                excelDto.setNumber(String.valueOf(++number));
                //赋值姓名
                excelDto.setName(excel .getName());
                //赋值时间
                excelDto.setTime(excel .getTime());
                //赋值备注
                excelDto.setText(excel .getText());
                //赋值图片(数据库路径为相对路径)
                excelDto.setImage(imageByUrl(excel .getImageUrl())); 

                //将每一行对象添加进导出excel的集合中
                list.add(excelDto);
            }


            String fileName = URLEncoder.encode("EasyExcel导出.xlsx", "UTF-8");
            OutputStream fileOutputStream = null;
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/x-download");
            response.setCharacterEncoding("UTF-8");
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            response.flushBuffer();
            fileOutputStream = response.getOutputStream();
            ExcelWriter excelWriter = EasyExcel.write(fileOutputStream, ExcelDto.class)
                    .build();
            WriteSheet sheet = EasyExcel.writerSheet(0, "sheet1").build();  //控制sheet页
            excelWriter.write(list, sheet);
            excelWriter.finish();
            fileOutputStream.flush();
            fileOutputStream.close();

        } catch (Exception e) {
            e.getMessage();
        }

    }

    /**
      *将string类型的url转换为文件
      */
    public File imageByUrl(String imageUrl) throws Exception{
        if (StringUtils.isBlank(imageUrl)){
            return null;
        }
        //指定读取的图片文件
        URI uri = new URI("file:///" + path+"/"+imageUrl);
        File file = new File(uri);
        //存在返回file否则返回null
        return file.exists() ?  file :  null;  
    }
相关推荐
破z晓7 分钟前
javascript 导出excel表
开发语言·javascript·excel
QQ_216962909640 分钟前
Spring Boot 养老院管理系统:从入住、护理到费用结算的全流程实现(源码可领)
java·spring boot·后端
cxoptics1 小时前
冰洲石分束器设计与应用
java
ck-joker1 小时前
M1 Pro跑LLM实测:Ollama+LangChain4j零成本本地大模型开发,比云端API快在哪?
java·语言模型
丙氨酸長鏈2 小时前
[Bukkit插件开发]手持发射器箭矢机枪 教学文档 面向Python/C#开发者入门Java与Bukkit API
java·python·c#
Nontee2 小时前
设计模式:模板方法与策略,从“每个字都认识“到能说清它们在干嘛
java·数据库·设计模式
我是唐青枫2 小时前
Java ReentrantLock 实战详解:比 synchronized 更灵活的可重入锁
java·开发语言
晨曦中的暮雨3 小时前
Virtual Thread 优化 Spring AI 阻塞式 LLM I/O:对照压测报告
java·并发·个人项目
AKA__Zas3 小时前
芝士算法(前缀和2.0)
java·数据结构·算法·leetcode·哈希算法·学习方法
caishenzhibiao3 小时前
降雨带波段点差 同花顺期货通指标
java·c语言·c#