通过EasyExcel导出excel文件并转为PDF或者其他
第一步导入所需依赖:
<!--这是为了导入e-iceblue依赖所需要的下载仓库地址,应该是可以配置的没试过,不然e-iceblue无法下载导入--><repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>http://repo.e-iceblue.com/nexus/content/groups/public/</url></repository></repositories><!--easyExcel--><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.6</version></dependency><!-- https://mvnrepository.com/artifact/e-iceblue/spire.xls.free --><dependency><groupId>e-iceblue</groupId><artifactId>spire.xls.free</artifactId><version>2.2.0</version></dependency>
导出功能,具体业务分离开,我图方便直接在里面写了,反正是测试类demo
package com.shiro.controller;import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.shiro.config.HorizontalCellStyleStrategyUtil;
import com.shiro.pojo.User;
import com.shiro.service.UserService;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;/*** @author qb* @version 1.0* go on!!!* @date 2021/9/16 8:56*/
@Controller
public class EasyExcelController {@Autowiredprivate UserService service;@RequestMapping("/excel")public String excelUser() {//这个是导出excel样式设置,简单设置一下可以根据自己需要进行设置,工具类下方贴出HorizontalCellStyleStrategy horizontalCellStyleStrategy = HorizontalCellStyleStrategyUtil.getHorizontalCellStyleStrategy();//所要导出的listList<User> list = service.selectList();//导出excel文件路径及名称String fileName = "D:\\javacx\\shiro\\src\\main\\resources\\static\\img\\excel\\user.xlsx";//定义set装所需要导出指定列,名称与实体类里面的字段一致,注意,需要添加注解自动映射,这里只是在映射中指定导出列Set<String> names = new HashSet<>();names.add("username");names.add("password");//通过EasyExcel导出为excel表格//registerWriteHandler为表格添加样式//includeColumnFiledNames指定导出列//sheet指定工作簿名称//doWrite将查询的list导出EasyExcel.write(fileName).head(User.class).registerWriteHandler(horizontalCellStyleStrategy).includeColumnFiledNames(names).sheet("sheet1").doWrite(list);//创建Workbook对象Workbook workbook = new Workbook();//打开指定路径的excel,如果注释掉本行,相当于新建excelworkbook.loadFromFile(fileName);//获取第一个sheet表格Worksheet sheet = workbook.getWorksheets().get(0);//随机起一个名称Random random = new Random();String pdf = "D:\\javacx\\shiro\\src\\main\\resources\\static\\img\\excel\\" + random.nextInt(10) + ".pdf";//将表格转为pdf//这里还有转其他格式方法,可以试试sheet.saveToPdf(pdf);//创建文件流,将导出的excel文件删除File file = new File(fileName);file.delete();return "/tt/t1";}
}
样式工具类
package com.shiro.config;import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;/*** @author qb* @version 1.0* go on!!!* @date 2021/9/16 10:28*/
public class HorizontalCellStyleStrategyUtil {public static HorizontalCellStyleStrategy getHorizontalCellStyleStrategy() {WriteCellStyle headWriteCellStyle = new WriteCellStyle();//设置头字体WriteFont headWriteFont = new WriteFont();headWriteFont.setFontHeightInPoints((short) 13);headWriteFont.setBold(true);headWriteCellStyle.setWriteFont(headWriteFont);//设置头居中headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//内容策略WriteCellStyle contentWriteCellStyle = new WriteCellStyle();//设置 水平居中contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);HorizontalCellStyleStrategy horizontalCellStyleStrategy=new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle);return horizontalCellStyleStrategy;}
}
实体类
package com.shiro.pojo;import com.alibaba.excel.annotation.ExcelProperty;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;import java.io.Serializable;/*** @author qb* @version 1.0* go on!!!* @date 2021/8/21 16:49*/
@Data
public class User implements Serializable {private static final long serialVersionUID = 1L;//导出注解,value第一个参数为第一行标题名,第二个为列名,index为导出排列顺序@ExcelProperty(value = {"用户信息表","编号"},index = 0)@TableId(value = "id",type = IdType.AUTO)private Integer id;@ExcelProperty(value = {"用户信息表","密码"},index = 2)private String password;@ExcelProperty(value = {"用户信息表","姓名"},index = 1)private String username;}
以上通过EasyExcel导出excel文件并转为PDF或者图片以及其他格式,是总结了其他博主的知识点实现的,记录一下,方便后续使用与改进。
通过EasyExcel导出excel文件并转为PDF或者其他相关推荐
- swagger easyExcel导出Excel文件打不开,文件损坏
alibaba/easyexcel文件导出 swagger easyExcel导出Excel文件打不开,文件损坏 文件下载打不开,将文件输出到本地发现可以打开,而且swagger下载的文件大小比本地文 ...
- Java 使用EasyExcel导出excel文件
Java 使用EasyExcel导出excel文件 一.引入pom依赖 二.导出实体 三. 生成excelController 四.效果 一.引入pom依赖 <dependency>< ...
- easyExcel导出excel文件并打包成zip压缩包下载
文件导出 专栏收录该内容 2 篇文章0 订阅 订阅专栏 package com.business.testExcelPort; import java.io.BufferedInputStream; ...
- 【JAVA】easyexcel 导出excel文件带多个图片
最终效果 pom版本 <developer> <groupId>com.alibaba</groupId><artifactId>easyexcel&l ...
- easyexcel导出excel文件合并相同单元格数据
1.引入easyexcel依赖 !-- 阿里开源easyexcel--><dependency><groupId>com.alibaba</groupId>& ...
- 使用easyExcel导出excel文件
1.导入jar包 <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel< ...
- springboot使用jxls导出excel___(万能通用模板)--- SpringBoot导入、导出Excel文件___SpringBoot整合EasyExcel模板导出Excel
springboot使用jxls导出excel 实现思路: 首先在springBoot(或者SpringCloud)项目的默认templates目录放入提前定义好的Excel模板,然后在具体的导出接口 ...
- easyexcel导入时读不到数据_SpringBoot中EasyExcel实现Excel文件的导入导出
前言 在我们日常的开发过程中经常会使用Excel文件的形式来批量地上传下载系统数据,我们最常用的工具是Apache poi,但是如果数据到底上百万时,将会造成内存溢出的问题,那么我们怎么去实现百万数据 ...
- EasyExcel入门:导出Excel文件
在开发过程中,我们经常需要导出Excel文件,一开始我使用的是Apache POI,通过创建XSSFWorkbook对象来导出Excel数据,但实际使用时发现需要占用较大的内存空间且导出时间较长,于是 ...
- EasyExcel 导入导出Excel文件
文章目录 写在前面 1.maven依赖 2.导入Excel文件 2.1.读取表格文件 2.2.如果有多个sheet表格 2.3.监听器封装(也可不封装) 2.4.读取数据格式化(实体类中添加注解) 3 ...
最新文章
- 有了这 4 款工具,老板再也不怕我写烂SQL了
- 提升机器学习数学,理论基础的7本著作(文末附资源下载!)
- ubuntu下python+tornado+supervisor+nginx部署
- 如何判断locals()变量或globals()变量是否存在或是否为空?
- win10中Android Studio (不含SDK) 安装后如何相关错误跳坑指南
- mysql主从延时这么长_MySQL主从延迟问题解决
- php无刷新跳转,用#号实现web页面的无刷新跳转
- 美国留学计算机网络技术,美国留学计算机专业详解
- 单张图片上传预览【超简洁,未完待续】
- sql 表变量 临时表_SQL表变量概述
- atomic的安全性?
- C# winform 跨线程修改界面
- 计算几何小结 我对计算几何的理解以及叉积和点积
- 史上最全股票指标图文详解
- TreeView 右键菜单
- 计算机制图专业是什么,专业设计制图需要什么样的电脑?制图电脑配置要求 (全文)...
- CAD怎么转化成PDF?手机就可以轻松解决
- 从特斯拉召回事件,窥探OTA汽车进化真面目
- win10桌面右下角网络图标中找不到网络
- 【微信小程序】shiro安全登录界面实现