Java技术:easypoi
2019/01/15 09:50 分类: 技术交流 浏览:100
1.导出excel-helloword
1.1导入相应jar包
- <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>
1.2准备domain
- public class EasyEmployee {
- /*
- name:列名
- width:列宽
- */
- @Excel(name="邮箱",width = 25)
- private String email;
- @Excel(name="用户名")
- private String username;
- @Excel(name = "年龄")
- private Integer age;
- /*format:时间格式*/
- @Excel(name = "生日",format = "yyyy-MM-dd")
- private Date bornDate = new Date();
- /* replace:值得替换 导出是{a_id,b_id} 导入反过来 就相当于如果值是true则显示男,如果值为false则显示女*/
- @Excel(name="性别",replace = {"男_true","女_false"})
- private Boolean sex;
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Date getBornDate() {
- return bornDate;
- }
- public void setBornDate(Date bornDate) {
- this.bornDate = bornDate;
- }
- public Boolean getSex() {
- return sex;
- }
- public void setSex(Boolean sex) {
- this.sex = sex;
- }
- }
1.3测试
- @Test
- public void testExport() throws Exception {
- List<EasyEmployee> employees = new ArrayList<>();
- EasyEmployee e1 = new EasyEmployee();
- e1.setUsername("李四");
- e1.setEmail("12@qq.com");
- e1.setAge(22);
- e1.setBornDate(new Date());
- e1.setSex(true);
- EasyEmployee e2 = new EasyEmployee();
- e2.setUsername("王耍耍");
- e2.setAge(33);
- e2.setBornDate(new Date());
- e2.setSex(false);
- e2.setEmail("22@qq.com");;
- employees.add(e1);
- employees.add(e2);
- /*
- 第一个参数: easypoi的基本配置(导出excel的基本配置)
- 第二个参数: 根据具体类型导出
- 第三个参数: 导出的数据
- */
- Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工列表信息","员工"),
- EasyEmployee .class, employees);
- FileOutputStream fileOutputStream = new FileOutputStream("employee.xlsx");
- workbook.write(fileOutputStream);
- fileOutputStream.close();
- }
1.4查看效果
2.导出excel-关联对象与图片2.1准备domain
- @ExcelTarget("emp")//相当于给该实体类取一个名字
- public class EasyEmployee {
- /*
- name:列名
- width:列宽
- */
- @Excel(name="邮箱",width = 25)
- private String email;
- @Excel(name="用户名")
- private String username;
- @Excel(name = "年龄")
- private Integer age;
- /*format:时间格式*/
- @Excel(name = "生日",format = "yyyy-MM-dd")
- private Date bornDate = new Date();
- /* replace:值得替换 导出是{a_id,b_id} 导入反过来 就相当于如果值是true则显示男,如果值为false则显示女*/
- @Excel(name="性别",replace = {"男_true","女_false"})
- private Boolean sex;
- //头像的url地址 savePath:你导入的图片存放的路径(对导入有效) type:导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本
- @Excel(name="头像", type = 2 ,height = 40,width = 30,savePath = "imgs/head")
- private String imgUrl;
- @ExcelEntity//标记是不是导出excel 标记为实体类,一遍是一个内部属性类,标记是否继续穿透,(简单理解就是我导出关联对象中对应的属性)
- private EasyDept dept;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public Boolean getSex() {
- return sex;
- }
- public void setSex(Boolean sex) {
- this.sex = sex;
- }
- public Date getBornDate() {
- return bornDate;
- }
- public void setBornDate(Date bornDate) {
- this.bornDate = bornDate;
- }
- public EasyDept getDept() {
- return dept;
- }
- public void setDept(EasyDept dept) {
- this.dept = dept;
- }
- public String getImgUrl() {
- return imgUrl;
- }
- public void setImgUrl(String imgUrl) {
- this.imgUrl = imgUrl;
- }
- }
- @ExcelTarget("dept")
- public class EasyDept {
- /*意思是:如果我导出EasyEmployee的时候一并把关联对象EasyDept导出来,那么我EasyDept中最终导出的名字就为部门
- 如果我单独只导出EasyDept那我的名字就叫做名字*/
- @Excel(name = "部门_emp,名字_dept")
- private String name;
- @Excel(name = "部门地址_emp,地址_dept")
- private String address;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public String toString() {
- return "EasyDept{" +
- "name='" + name + '\'' +
- ", address='" + address + '\'' +
- '}';
- }
- }
2.2测试
- @Test
- public void testExport() throws Exception {
- List<EasyEmployee> employees = new ArrayList<>();
- EasyEmployee e1 = new EasyEmployee();
- e1.setUsername("李四");
- e1.setEmail("12@qq.com");
- e1.setAge(22);
- e1.setBornDate(new Date());
- e1.setSex(true);
- EasyDept dept = new EasyDept();
- dept.setName("开发部");
- dept.setAddress("东莞");
- e1.setDept(dept);
- e1.setImgUrl("imgs/tx.jpg");
- EasyEmployee e2 = new EasyEmployee();
- e2.setUsername("王耍耍");
- e2.setAge(33);
- e2.setBornDate(new Date());
- e2.setSex(false);
- e2.setEmail("22@qq.com");
- dept = new EasyDept();
- dept.setName("销售部");
- dept.setAddress("北京");
- e2.setDept(dept);
- e2.setImgUrl("imgs/pp.jpg");
- employees.add(e1);
- employees.add(e2);
- /*
- 第一个参数: easypoi的基本配置(导出excel的基本配置)
- 第二个参数: 根据具体类型导出
- 第三个参数: 导出的数据
- */
- Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工列表信息","员工"),
- EasyEmployee .class, employees);
- FileOutputStream fileOutputStream = new FileOutputStream("employee.xlsx");
- workbook.write(fileOutputStream);
- fileOutputStream.close();
- }
3.单独导出部门
- @Test
- public void testExport1() throws Exception {
- List<EasyDept> depts = new ArrayList<>();
- EasyDept dept = new EasyDept();
- dept.setName("公关部");
- dept.setAddress("东莞");
- depts.add(dept);
- dept = new EasyDept();
- dept.setName("销售部");
- dept.setAddress("北京");
- depts.add(dept);
- Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
- EasyDept .class, depts);
- FileOutputStream fileOutputStream = new FileOutputStream("dept.xlsx");
- workbook.write(fileOutputStream);
- fileOutputStream.close();
- }
4.导入功能4.1测试
- @Test
- public void testImport() throws Exception{
- //导入的基本信息
- ImportParams params = new ImportParams();
- //导入标题占一行
- params.setTitleRows(1);
- //导入头占一行
- params.setHeadRows(1);
- long start = new Date().getTime();
- List<EasyEmployee> list = ExcelImportUtil.importExcel(
- new FileInputStream("employee.xlsx"),//导入指定的文件
- EasyEmployee.class,//导入的数据转为指定的类型
- params);//导入的基本信息的设置
- for (EasyEmployee easyEmployee : list) {
- System.out.println(easyEmployee);
- }
- }
4.2效果
#标签:java