1.认识
Java操作办公软件:jxl、poi
数据读取用POI,只要不涉及单元格样式的写入使用jxl效率高点。POI读取效率高,但是大数据量写入Excel效率比jxl慢。
jxl只使用03版本、po使用所有版本
2.easypoi需要导入的jar包
1 <dependency> 2 <groupId>cn.afterturn</groupId> 3 <artifactId>easypoi-base</artifactId> 4 <version>3.2.0</version> 5 </dependency> 6 <dependency> 7 <groupId>cn.afterturn</groupId> 8 <artifactId>easypoi-web</artifactId> 9 <version>3.2.0</version> 10 </dependency> 11 <dependency> 12 <groupId>cn.afterturn</groupId> 13 <artifactId>easypoi-annotation</artifactId> 14 <version>3.2.0</version> 15 </dependency>
1 <!-- JSR 303 规范验证包 Excel验证--> 2 <dependency> 3 <groupId>org.hibernate</groupId> 4 <artifactId>hibernate-validator</artifactId> 5 <version>5.2.4.Final</version> 6 </dependency>
3.在需要操作的实体类进行配置注解
@Excel注解为需要导入或是导出的数据、name为表头、
注意:type=2表示为图片、format可以设置日期格式
2 public class Employee extends BaseDomain{ 3 @Excel(name = "用户名") 4 @NotNull(message = "用户名不能为空") 5 private String username; 6 private String password; 7 @Excel(name = "邮箱",width = 20) 8 private String email; 9 @Excel(name = "年龄") 10 @Max(value = 60,message = "年龄不能大于60") 11 @Min(value = 18,message = "年龄不能小于18") 12 private Integer age; 13 @Excel(name = "头像",type = 2,height = 30) 14 private String headImage;
4.在applicationContex中配置view
1 <!--Excel导出的视图解析器--> 2 <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"></bean> 3 <!--扫描Excel的view--> 4 <context:component-scan base-package="cn.afterturn.easypoi.view"/>
5.导出
1 public String exportExcel(ModelMap map, SystemlogQuery query){ 2 //拿到数据 3 List<Systemlog> list = systemlogService.findAll(); 4 ExportParams params = new ExportParams("日志数据", "日志表", ExcelType.XSSF); 5 // params.setFreezeCol(5); //冻结 那条绿线 6 map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合 7 map.put(NormalExcelConstants.CLASS, Systemlog.class);//导出实体 8 map.put(NormalExcelConstants.PARAMS, params);//参数 9 map.put(NormalExcelConstants.FILE_NAME, "日志信息");//文件名称 10 return NormalExcelConstants.EASYPOI_EXCEL_VIEW;//需要配置新的视图解析器并设置优先级和扫描 11 }
6.导入
1 //empFile名字与前台一致 2 public String importXlsx(MultipartFile empFile,HttpServletResponse response) throws Exception{ 3 //获取参数 4 ImportParams params = new ImportParams(); 5 params.setHeadRows(1); //不要这行 6 //验证开启 7 params.setNeedVerfiy(true); 8 //自定义验证 9 params.setVerifyHandler(employeeExcelVerifyHandler); 10 //导入参数 11 /*List<Employee> list = ExcelImportUtil.importExcel( 12 empFile.getInputStream(), 13 Employee.class, params);*/ 14 ExcelImportResult<Employee> result = ExcelImportUtil.importExcelMore( 15 empFile.getInputStream(), 16 Employee.class, params); 17 18 //获取正确导入的集合 19 List<Employee> list = result.getList(); 20 for (Employee employee : list) { 21 employee.setPassword("123"); //默认密码123 22 if(employee.getDepartment()!=null) { 23 //设置部门 24 Department department = departmentService.findByName(employee.getDepartment().getName()); 25 employee.setDepartment(department); 26 } 27 28 employeeService.save(employee); 29 } 30 //如果导入失败 31 if(result.isVerfiyFail()){ 32 //获取导入失败的员工的Excel 33 Workbook failWorkbook = result.getFailWorkbook(); 34 //导出Excel表 35 //获取输出流 36 ServletOutputStream fos = response.getOutputStream(); 37 //响应类型 38 response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型 39 //告诉浏览器这是下载 40 response.setHeader("Content-disposition", "attachment;filename=errorx.xlsx"); 41 //不要缓存 42 response.setHeader("Pragma", "No-cache"); 43 failWorkbook.write(fos); 44 fos.flush(); 45 fos.close(); 46 } 47 return "import"; 48 }