springboot2.x Easy poi 导入导出

链接: api文档.

pom

   <!--easypoi-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>3.3.0</version>
        </dependency>

application.properties

spring.main.allow-bean-definition-overriding=true

Excel 注解导出

@Data
public class ExStudent implements Serializable {
    @Excel(name = "姓名")
    private String name;
    @Excel(name = "年龄",orderNum = "1")
    private Integer age;
    @Excel(name = "性别",replace = { "男_0", "女_1" ,"_null"},orderNum = "2")
    private Integer sex;
    @Excel(name = "地址",orderNum = "3")
    private String addr;
    @Excel(name = "高数",groupName = "学科",numFormat = "#.#",orderNum = "4")
    private Double gs ;
    @Excel(name = "数据结构",groupName = "学科",orderNum = "5")
    private Double sjjg;
    @Excel(name = "创建时间",orderNum = "6",format = "yyyy-MM-dd")
    private Date createTime;
    @Excel(name = "备注",width = 50,orderNum = "7")
    private String nt;
}
@RequestMapping("/exportExcel")
public void exportExcel(HttpServletResponse response) {
     List<ExStudent> list = new ArrayList<>();
     list.add(new ExStudent("tom",28,1,"南山",95.6d,96.66d,new Date(),"这是一条备注"));
     Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("这是title","sheet名称"), ExStudent.class, list);
     try {
         // 导出配置
         response.reset();
         response.setHeader("content-disposition",
                 "attachment;filename=" + new String(("导出测试".getBytes()), "ISO8859-1") + ".xlsx");
         response.setContentType("application/vnd.ms-excel");
         response.setCharacterEncoding("UTF-8");
         workbook.write(response.getOutputStream());
     } catch (IOException e) {
         e.printStackTrace();
     }
 }

导出:
在这里插入图片描述

Excel模板导出

模板 指令介绍

空格分割
三目运算 {{test ? obj:obj2}}
n: 表示 这个cell是数值类型 {{n:}}
le: 代表长度{{le:()}} 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
fd: 格式化时间 {{fd:(obj;yyyy-MM-dd)}}
fn: 格式化数字 {{fn:(obj;###.00)}}
fe: 遍历数据,创建row (下方没有数据
!fe: 遍历数据不创建row
$fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入 (下方有数据
#fe: 横向遍历
v_fe: 横向遍历值
!if: 删除当前列 {{!if:(test)}}
单引号表示常量值 ‘’ 比如’1’ 那么输出的就是 1
&NULL& 空格
&INDEX& 表示循环中的序号,自动添加
]] 换行符 多行遍历导出
sum: 统计数据
cal: 基础的±X% 计算
dict: 字典
i18n: 国际化
在这里插入图片描述
在这里插入图片描述

@RequestMapping("/exportExcel")
public Map exportExcel(HttpServletResponse response) {
      Map<String, Object> map = new HashMap<>();
      //1、获取excel模板 TemplateExportParams("url",true)多sheet页导出
      TemplateExportParams exportParams = new TemplateExportParams("excel/Easypoi导出模板.xlsx");
      //2、获取数据 excelList mapList
      map.put("dataList", excelList);
      //3、执行Excel 导出
      Workbook wb = ExcelExportUtil.exportExcel(exportParams, map);
      //4、写入输出流
      try {
          // 导出配置
          response.reset();
          response.setHeader("content-disposition",
                  "attachment;filename=" + new String(("导出测试".getBytes()), "ISO8859-1") + ".xlsx");
          response.setContentType("application/vnd.ms-excel");
          response.setCharacterEncoding("UTF-8");
          wb.write(response.getOutputStream());
      } catch (Exception e) {
          e.printStackTrace();
      } finally {

      }
      System.out.println("导出完成......");
      return null;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值