热门推荐
立即入驻

easypoi之excel导入导出教程参考

Java2年前 (2022)发布 达西克
2.9K 0 0

easypoi 官方文档参考

easypoi的介绍

easypoi功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员

就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板

语言(熟悉的表达式语法),完成以前复杂的写法

适用框架、范围

SpringBoot,SSH(两大框架在实际项目中都有用到)

非maven系列的项目得自己导入jar包了

开始使用(SpringBoot+mybatis plus)

1.pom文件导入依赖

<!--EasyPoi导入导出-->
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-base</artifactId>
	<version>3.0.3</version>
</dependency>
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-web</artifactId>
	<version>3.0.3</version> </dependency>
<dependency>
	<groupId>cn.afterturn</groupId>
	<artifactId>easypoi-annotation</artifactId>
	<version>3.0.3</version>
</dependency>

2.实体类加注解,绑定映射关系

下面有使用到的注解作用和说明

@TableName("CERP_TEST_USER") //mybatis plus框架的注解,可忽略
@Entity //mybatis plus框架的注解,可忽略
@ExcelTarget("cerpTestUser")
public class CerpTestUser extends Model<CerpTestUser> {

private static final long serialVersionUID=1L;

    @TableId("ID")	//mybatis plus框架的注解,可忽略
    @Excel(name = "ID",width = 5.0,type = 10)
    private Long id;

    @TableField("NAME") //mybatis plus框架的注解,可忽略
    @Excel(name = "NAME",width = 30.0)
    private String name;

    @TableField("WORK_CODE") //mybatis plus框架的注解,可忽略
    @Excel(name = "WORK_CODE")
    private String workCode;

    @TableField("EMAIL")  //mybatis plus框架的注解,可忽略
    @Excel(name = "EMAIL",width = 30.0)
    private String email;

    @TableField(value = "STATUS",fill = FieldFill.INSERT) //mybatis plus框架的注解,可忽略
    @Excel(name = "STATUS",width = 20.0, replace = { "正常_0", "停用_1", "未知_2" },suffix = "账号")
    private String status;

    @TableField(value = "DEL_FLAG",fill = FieldFill.INSERT) //mybatis plus框架的注解,可忽略
    private Integer delFlag;

    @TableField(value = "CREATE_DATE",fill = FieldFill.INSERT) //mybatis plus框架的注解,可忽略
    private Date createDate;

    @TableField(value = "MODIFY_DATE",fill = FieldFill.INSERT_UPDATE) //mybatis plus框架的注解,可忽略
    private Date modifyDate;

    //getter,setter
}
注解说明(只介绍经常有用到的,具体参考官方文档)

@Excel

属性 类型 默认值 功能
name String null 列名,用于绑定字段
width double 10 excel导出时显示宽度
type int 1 导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本,数字类型不指定type导出excel时会有警告
replace String[] {} 值的替换 导出是{a_id,b_id} 导入反过来,一般用于字典导出时转换,如”男_0″, “女_1”,导出时候 0->男 ,导入的时候 男->0
suffix String “” 在值最后面加后缀,例如百分数,可指定%

@ExcelTarget

这个是作用于最外层的对象,描述这个对象的id,以便支持一个对象可以针对不同导出做出不同处理

3.使用导入导出工具类

下载ExcelUtil

4.Controller层

导入

/**
 * myFile:前台传过来的excel导入数据的附件
 */
@RequestMapping("/uploadFile")
    public ModelAndView batchImportUser(@RequestParam("myFile") MultipartFile myFile){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("index");
        //importExcel(file,titleRows,headerRows,class)
        //titleRows:标题所占行数  headerRows:头部,顶部标题行数
        List<CerpTestUser> cerpTestUsers = ExcelUtils.importExcel(myFile, 1, 1, CerpTestUser.class);
        try{
            boolean b = testUserService.insertBatch(cerpTestUsers);	//mybatis plus的批量保存
        }catch (Exception e){
            System.out.println(e.getCause());
            mv.setViewName("error");
        }
        return mv;
    }

导出

@RequestMapping("/batchExportUser")
    public void batchExportUser(HttpServletResponse response) throws UnsupportedEncodingException {
        EntityWrapper wrapper=new EntityWrapper();
        List<String> orderCoulmns =new ArrayList<>();
        orderCoulmns.add("id");
        wrapper.orderAsc(orderCoulmns);
        List list = testUserService.selectList(wrapper);//查询出list
        //exportExcel(list,title,sheetName,class,fileName,isCreateHeader,response)
        //title:标题    isCreateHeader:是否显示头部的标题
        //fileName:注意一定要指定excel格式,不然导出会出现问题
        ExcelUtils.exportExcel(list,"Export User List","sheet1",CerpTestUser.class,"export_list.xls",true,response);
    }

非maven项目踩坑记录

在之前使用非maven系列项目时使用easypoi(Spring+struts2+hibernate),光导入三个easypoi的jar包是不够的,上网查了下帖子总结下来一共是要以下几个jar包easypoi之excel导入导出教程参考

© 版权声明

相关文章

暂无评论

暂无评论...