通过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 {
    @Autowired
    private UserService service;

    @RequestMapping("/excel")
    public String excelUser() {
        //这个是导出excel样式设置,简单设置一下可以根据自己需要进行设置,工具类下方贴出
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = HorizontalCellStyleStrategyUtil.getHorizontalCellStyleStrategy();
        //所要导出的list
        List<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,如果注释掉本行,相当于新建excel
        workbook.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或者图片以及其他格式,是总结了其他博主的知识点实现的,记录一下,方便后续使用与改进。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值