前言:
JXL和POI都是操作办公软件[ ex: excel与word ] 的框架,可以用来完成相应的Excel操作
但EasyPoi可以非常轻松的完成Excel的操作。EasyPoi,主打简单,不过功用依然OK(绝对够用)。
使用之前先来了解一下注解属性吧!
字段 |
作用,示例 |
默认值 |
name |
导入导出字段名称比如: name = "学生姓名" |
无 |
width |
导出字段宽度(可以每个设置), width = 30 |
10 |
height |
导出高度(一个设置全局生效), height = 20 |
10 |
replace |
替换值,比如: replace = {"男_1","女_2"} |
[] |
type |
导出字段类型导出类型 1 是文本 2 是图片,3是函数默认是文本 |
1 |
imageType |
图片类型, 导出类型 1 从file读取 2 是从数据库中读取 |
1 |
savePath |
图片保存路径 |
upload |
orderNum |
排序 |
0 |
format |
时间格式化 |
空 |
字段 |
作用,示例 |
默认值 |
name |
导入导出字段名称比如: name = "学生姓名" |
无 |
orderNum |
排序 |
0 |
type |
导入时创建List的实现类 |
ArrayList |
字段 |
作用,示例 |
默认值 |
name |
导入导出字段名称比如: name = "学生姓名" |
无 |
ExcelIgnore : 忽略属性 (主要作用就是防止无限循环)
ExcelTarget :导出目标,导出对象,表示当前导出的对象,表示导出的ID,为字段选择做依据
字段 |
作用,示例 |
默认值 |
interHandler |
是不是使用接口处理 |
false |
notNull |
非空 |
false |
isMobile |
手机号 |
false |
isTel |
座机号 |
false |
isEmail |
|
false |
minLength |
最小长度 |
-1 |
maxLength |
最大长度 |
-1 |
regex |
正则表达式 |
空 |
regexTip |
正则错误提示信息 |
数据不符合规范 |
如何使用easypoi ??
第一步:项目中引入easypoi (温馨小提示:这里我是maven项目的引入方式哦!)
<!-- easypoi的支持 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
第二步:操作
准备的2个bean类
@ExcelTarget("emp")
public class POIEmployee {
@Excel(name = "用户名")
private String name;
@Excel(name = "邮件",width = 20)
private String email;
@Excel(name="年龄")
private Integer age ;
@Excel(name="性别",replace ={"男_true","女_false"})
private Boolean sex;
@Excel(name = "出生日期",format="yyyy-MM-dd",width = 20)
private Date bronDate = new Date();
@ExcelEntity
private POIDepartment department;
@Excel(name="头像",type = 2,height = 20,savePath = "POITestImage/img")
private String headImage;
//下面是getter/setter方法和toString方法...
}
@ExcelTarget("dept")
public class POIDepartment {
@Excel(name = "部门名称_emp,名称_dept") //表示在POIEmployee的那张表中显示的名称是部门名称,此表中显示的名称-》相当于是在不同表中取不同的一个名字
private String name;
@Excel(name="地址_dept")
private String address;
//下面是getter/setter方法和toString方法...
}
模拟数据库进行Excel文件数据的导入和导出
//模拟数据库操作
public class EasyPoiTest {
@Test //Excel文件数据的导出
public void testExport() throws Exception {
POIDepartment d1 = new POIDepartment();
d1.setName("IT部");
d1.setAddress("四川成都");
POIDepartment d2 = new POIDepartment();
d2.setName("PS部");
d2.setAddress("四川攀枝花");
List<POIDepartment> list = new ArrayList<>();
list.add(d1);
list.add(d2);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
POIDepartment.class, list);
//写一个文件输出流,把内存中的excel文件写出去
FileOutputStream fos = new FileOutputStream("department.xlsx");
workbook.write(fos);
fos.close();
}
@Test //Excel文件数据的导出
public void testExport2() throws Exception {
POIDepartment department = new POIDepartment();
department.setName("IT部");
department.setAddress("四川成都");
//准备导出的数据
POIEmployee e1 = new POIEmployee();
e1.setName("张三");
e1.setEmail("[email protected]");
e1.setAge(18);
e1.setSex(true);
e1.setHeadImage("POITestImage/1.jpg"); //头像
e1.setDepartment(department);
POIEmployee e2 = new POIEmployee();
e2.setName("李四");
e2.setEmail("[email protected]");
e2.setAge(20);
e2.setSex(false);
e2.setHeadImage("POITestImage/2.jpg"); //头像
e2.setDepartment(department);
List<POIEmployee> list = new ArrayList<>();
list.add(e1);
list.add(e2);
/**
* excel生成的工具类:
* ExportParams:一些基本配置(不需要的)
* POIEmployee.class :导出的对象的类型
* list:要导出的数据
*/
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工信息", "员工表"),
POIEmployee.class, list);
//写一个文件输出流,把内存中的excel文件写出去
FileOutputStream fos = new FileOutputStream("employee.xlsx");
workbook.write(fos);
fos.close();
}
@Test //Excel文件数据的导入
public void testImport() throws Exception {
ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
List<POIEmployee> list = ExcelImportUtil.importExcel(
new File("employee.xlsx"),//employee.xlsx为需要导入的文件名
POIEmployee.class, params);
list.forEach(e -> System.out.println(e));
}
}
效果图:
导出的2个表
导入的表,控制打印信息:
最后截图看下我的表和图片的位置吧:
更多的功能可参考EasyPoi文档教程:http://easypoi.mydoc.io/#text_173307