1 操作excel的认识
Java操作excel有两个框架:POI,JXL
办公软件分两个版本: 03及以前,07及以后
2 创建Excel文件
创建文件(workbook) -> 创建表(sheet) -> 创建行(row) -> 创建格子(写数据)(cell)
/**
@Test
public void testCreate() throws Exception{
/**
* 一.创建一个Excel文件(SXSSFWorkbook) 放在内存中
*/
SXSSFWorkbook workbook = new SXSSFWorkbook();
/**
* 二.创建一张表
*/
Sheet sheet = workbook.createSheet("蜜蜂");
/**
* 三.添加相应的行
*/
for (int i = 1; i <= 9; i++) {
Row row = sheet.createRow(i-1);
//四.添加相应的列(格子)
for (int j = 1; j <= i; j++) {
Cell cell = row.createCell(j-1);
//五.格子里面添加数据
cell.setCellValue(i+"*"+j+"="+(i*j));
}
}
//最后一步,输出
FileOutputStream out = new FileOutputStream("99.xlsx");
workbook.write(out);
out.close();
}
3 读取Excel文件
读取文件(workbook) -> 获取表(sheet) -> 获取行(row) -> 获取格子(拿到数据)(cell)
/**
* 通过Java代码读数据
* @throws Exception
*/
@Test
public void testRead() throws Exception{
// Use a file
// Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));
// 一.读取到相应的excel文件(放到内存中)
Workbook wb = WorkbookFactory.create(new FileInputStream("emp.xlsx"));
// 二.获取到第一张表
Sheet sheet = wb.getSheetAt(0);
// 三.获取到行
//3.1 拿到总行数(最后一行就是总行数)
int lastRowNum = sheet.getLastRowNum();
for (int i = 1; i <= lastRowNum; i++) {
//3.2 拿到当前行
Row row = sheet.getRow(i);
//四.拿到格子中的数据
//4.1 拿到总列数(多少个格子)
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
//4.2 获取到格子数据
Cell cell = row.getCell(j);
//cell.getNumericCellValue()
// System.out.println(cell.getCellType());
System.out.println(cell.getStringCellValue());
}
}
}
官方文档:http://easypoi.mydoc.io/
1 导入easypoi的包
注意:咱们项目中的poi包删除(可能产生冲突)
<!-- 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 EasyPOI的测试
2.1 实体类
POIEmployee:员工
public class POIEmployee {
private Long id;
//生成的excel的头u啊名称
@Excel(name = "用户名")
private String username;
//width:格子的宽度
@Excel(name = "邮箱",width=20)
private String email;
@Excel(name = "年龄")
private Integer age;
//replace:替换 true就会显示男,false显示女
@Excel(name = "性别", replace = { "男_true", "女_false" })
private Boolean sex;
//format:导入和导出的日期格式
@Excel(name = "出生日期",width = 20,format="yyyy-MM-dd HH:mm:ss")
private Date bornDate = new Date();
//type=2代表它是一张图片
@Excel(name = "头像",type = 2 ,width = 20 , height = 20)
private String headImage;
@ExcelEntity
private POIDepartment department;
...
}
POIDepartment:部门
2 测试代码
public class EasyPOITest {
@Test
public void test() throws Exception{
POIDepartment d1 = new POIDepartment();
d1.setId(1L);
d1.setName("俱乐部");
POIEmployee e1 = new POIEmployee();
e1.setId(1L);
e1.setUsername("小王");
e1.setEmail("[email protected]");
e1.setAge(12);
e1.setSex(true);
e1.setHeadImage("images/1.png");
e1.setDepartment(d1);
POIEmployee e2 = new POIEmployee();
e2.setId(2L);
e2.setUsername("小李");
e2.setEmail("[email protected]");
e2.setAge(18);
e2.setSex(false);
e2.setHeadImage("images/3.png");
e2.setDepartment(d1);
//准备数据(以后是直接读取这些数据)
List<POIEmployee> list = new ArrayList<>();
list.add(e1);
list.add(e2);
/**
* 参数一:导出的一些属性
* title:标题 sheetName:表名
* 参数二:导出的数据类型
* 参数三:导出的数据
*/
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
POIEmployee.class, list);
//最后一步,输出
FileOutputStream out = new FileOutputStream("emphaha.xlsx");
workbook.write(out);
out.close();
}
@Test
public void testDept() throws Exception{
POIDepartment d1 = new POIDepartment();
d1.setId(1L);
d1.setName("俱乐部");
POIDepartment d2 = new POIDepartment();
d2.setId(2L);
d2.setName("小卖部");
//准备数据(以后是直接读取这些数据)
List<POIDepartment> list = new ArrayList<>();
list.add(d1);
list.add(d2);
/**
* 参数一:导出的一些属性
* title:标题 sheetName:表名
* 参数二:导出的数据类型
* 参数三:导出的数据
*/
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),
POIDepartment.class, list);
//最后一步,输出
FileOutputStream out = new FileOutputStream("depthaha.xlsx");
workbook.write(out);
out.close();
}
}
导出功能
1.1 employee/index.jsp准备导出按钮
<form id="searchForm" action="/employee/export" method="post">
...
<button class="easyui-linkbutton" iconCls="icon-search">导出</button>
</form>
1.2 准备好view(beanName的视图解析器)
applicationContext-mvc.xml
<!--配置一下beanName的视图解析器
p:order="0":设置属性优先级
-->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0" />
<!--
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0" />
</bean>
-->
<!-- 对于easypoi中准备发的视图(view)的扫描 -->
<context:component-scan base-package="cn.afterturn.easypoi.view" />
1.3 EmployeeController完成后台的导出
/**
@RequestMapping("/export")
public String export(EmployeeQuery query,ModelMap map, HttpServletRequest request){
//根据查询条件拿到所有数据
List<Employee> list = employeeService.queryAll(query);
//导出的属性
ExportParams params = new ExportParams("员工数据", "员工表", ExcelType.XSSF);
//params.setFreezeCol(2); 冻结
//request:获取到真实路径
String realPath = request.getServletContext().getRealPath("");
System.out.println(realPath);
list.forEach(e->{
e.setHeadImage(realPath+e.getHeadImage());
});
map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
map.put(NormalExcelConstants.CLASS, Employee.class);//导出实体
map.put(NormalExcelConstants.PARAMS, params);//参数
map.put(NormalExcelConstants.FILE_NAME, "employee");//文件名称
//根据返回的名称去找一个bean对象(在SpringMVC称之为view)
// /WEB-INF/views/easypoiExcelView
// 我们直接要文档的的操作是没有成功的 2.文档的情况和我们项目中的情况有一点区别
return NormalExcelConstants.EASYPOI_EXCEL_VIEW;//View名称 "easypoiExcelView"
}
导入功能
注意,必需配置上传解析器,名字必需叫 multipartResolver
2.1 准备导入页面import.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%@ include file="/WEB-INF/views/head.jsp"%>
</head>
<body>
<%--
上传只能是post请求
上传必需设置 : enctype="multipart/form-data"
--%>
<!-- 上传必需是:post,enctype="multipart/form-data"-->
<form action="/import/employeeXlsx" method="post" enctype="multipart/form-data">
<input class="easyui-filebox" name="empFile" style="width:80%"
data-options="prompt:'选择一个文件...',buttonText: '选择文件'" />
<button class="easyui-linkbutton">导入</button>
</form>
</body>
</html>
2.2 后台导入功能
@Controller
@RequestMapping("/import")
public class ImportController extends BaseController {
@Autowired
private IDepartmentService departmentService;
@RequestMapping("/index")
public String index(){
return "import";
}
/**
* 上传的文件使用 MultipartFile类型来接收
* @param empFile
* @return
*/
@RequestMapping("/employeeXlsx")
public String employeeXlsx(MultipartFile empFile) throws Exception {
// System.out.println(empFile);
// System.out.println(empFile.getName()); //empFile:上传控件的名称
// System.out.println(empFile.getOriginalFilename()); //emp-poi.xlsx:上传文件的名称
// System.out.println(empFile.getContentType()); //mime类型 office excel 2007
// System.out.println(empFile.getSize()); //文件大小
//我们需要拿到上传的文件(输入流)
// empFile.getInputStream()
//一.使用EasyPoi获取文件数据
//1.1 设置参数
ImportParams params = new ImportParams();
//params.setTitleRows(1);
params.setHeadRows(1);
// 1.2 拿到文件中的数据
List<Employee> list = ExcelImportUtil.importExcel(empFile.getInputStream(),
Employee.class, params);
//二.保存员工数据
list.forEach(e->{
//导入时加上一个默认密码 -> 大家可以去研究一个扩展功能:第一次登录要求修改密码
e.setPassword("123");
//通过部门名称拿到部门数据(要有id)
Department dept = departmentService.findByName(e.getDepartment().getName());
//把部门设置到员工数据中
e.setDepartment(dept);
employeeService.save(e);
} );
return "import";
}
}
导入验证功能
3.1 导入验证包支持
<!-- JSR 303 规范验证包 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
3.2 domain中添加验证方法
3.2.1 注解普通验证
@Entity
@Table(name = "employee")
public class Employee extends BaseDomain {
@Excel(name = "用户名")
@NotNull(message = "用户名不能空")
private String username;
private String password;
@Excel(name = "年龄")
@Max(value = 100)
@Min(value = 18)
private Integer age;
@Excel(name = "邮箱",width = 20)
@NotNull
private String email;
...
}
3.2.2 自定义验证
实现IExcelVerifyHandler
把这个类交给Spring管理(千万不要忘了让Spring去扫描到它)
/**
@Component
public class EmployeeExcelVerifyHandler implements IExcelVerifyHandler<Employee> {
@Autowired
private IEmployeeService employeeService;
/**
*
* ExcelVerifyHandlerResult
* suceess :代表验证成功还是失败(如果用户名重复,就代表失败)
* msg:失败的原因
*/
@Override
public ExcelVerifyHandlerResult verifyHandler(Employee employee) {
ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
//如果根据用户名获取到用户,代表这个用户已经存在
Employee tempEmp = employeeService.findByUsername(employee.getUsername());
if(tempEmp!=null){
result.setSuccess(false);
result.setMsg("用户名重复");
}
return result;
}
}
3.2.3 实现验证功能
@Controller
@RequestMapping("/import")
public class ImportController extends BaseController {
@Autowired
private IEmployeeService employeeService;
@Autowired
private IDepartmentService departmentService;
@Autowired
private EmployeeExcelVerifyHandler employeeExcelVerifyHandler;
@RequestMapping("/index")
public String index(){
return "import";
}
@RequestMapping("/employeeXlsx")
public String employeeXlsx(MultipartFile empFile, HttpServletResponse response) throws Exception {
//一.使用EasyPoi获取文件数据
ImportParams params = new ImportParams();
params.setHeadRows(1);
params.setNeedVerfiy(true); //设置验证支持
params.setVerifyHandler(employeeExcelVerifyHandler); //设置一个验证处理器
//二.获取excel中的数据,封装成了一个结果对象(有很多东西)
ExcelImportResult<Employee> result = ExcelImportUtil.importExcelMore(
empFile.getInputStream(),
Employee.class, params);
//三.获到正确的数据,并把它们保存到数据库
List<Employee> list = result.getList();
list.forEach(e->{
e.setPassword("123");
Department dept = departmentService.findByName(e.getDepartment().getName());
e.setDepartment(dept);
employeeService.save(e);
});
//四.如果有错误,把错误数据返回到前台(让前台下载一个错误的excel)
//4.1判断是否有错误
if(result.isVerfiyFail()){
//4.2拿到错误的文件薄
Workbook failWorkbook = result.getFailWorkbook();
//把这个文件导出
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //mime类型
response.setHeader("Content-disposition", "attachment;filename=error.xlsx"); //告诉浏览下载的是一个附件,名字叫做error.xlsx
response.setHeader("Pragma", "No-cache");//设置不要缓存
OutputStream ouputStream = response.getOutputStream();
failWorkbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
return "import";
}
}
很多表结构差不多,没有什么特别的业务,我们可以把这些表做一个抽取,变成两张表
下面这几个东西的CRUD能不能搞定