springboot中使用easypoi导出excel表格

  • 引入pom依赖
	          <dependency>
                  <groupId>cn.afterturn</groupId>
                  <artifactId>easypoi-spring-boot-starter</artifactId>
                  <version>4.2.0</version>
              </dependency>
              <dependency>
                  <groupId>cn.afterturn</groupId>
                  <artifactId>easypoi-base</artifactId>
                  <version>4.2.0</version>
              </dependency>
              <dependency>
                  <groupId>cn.afterturn</groupId>
                  <artifactId>easypoi-web</artifactId>
                  <version>4.2.0</version>
              </dependency>
              <dependency>
                  <groupId>cn.afterturn</groupId>
                  <artifactId>easypoi-annotation</artifactId>
                  <version>4.2.0</version>
              </dependency>

两种方式(第一种是不通过模版进行导出,第二种是通过模版进行导出)

  • 无模版导出(使用注解)

无模版导出依赖对象

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;
import java.io.Serializable;

@Data
public class User implements Serializable{
	
	/**
	 * 姓名
	 **/
	 @Excel(name = "姓名", width = 30 ,isImportField = "true_st")
	private String userName;

    /**
	 * 手机号
	 **/
	 @Excel(name = "手机号", width = 30 ,isImportField = "true_st")
	private String phone;
}
    public void export(){
    	//文件里面内容
        List<User> pageList = certPersonMapper.selectList();
        Workbook workbook = ExcelExportUtil.exportExcel(
                new ExportParams("xx系统","人员信息"),User.class, pageList);
        try {
            //文件生成路径
            FileOutputStream fos = new FileOutputStream("E:\\person_download.xls");
            workbook.write(fos);
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  • 使用模版导出
    public void exportCustomExcel(){
        //1.获取废品的excel模板
        TemplateExportParams params = new TemplateExportParams("E:\\人员信息.xlsx");
        //2.获取所有过磅数据
        List<User> allMap = certPersonMapper.selectList();

        Map<String, Object> map = new HashMap<String, Object>(100);
        map.put("poundLogList", allMap);

        //3.执行excel导出
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);

        //4.创建文件存储路径
        File saveFile = new File("E:\\");
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }
        FileOutputStream fos;
        String filePath;
        try {
            //4.写入文件
            filePath = saveFile + "\\person_download1.xls";
            fos = new FileOutputStream(filePath);
            workbook.write(fos);
            fos.close();
        } catch (Exception e) {
            log.error("IOException={}", e.getMessage(), e);
        }
    }
  • 模本内容
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值