easypoi技术是对apachpoi技术的一个包装,详情可以看看作者的开发文档EasyPoi教程,废话不多说,我们的任务是:
- 完成一个web页面
- 设计excel导入大批量数据(>200W)至数据库的功能
- 设计数据库导出大批量数据(>200W)至excel的功能
- 数据页面展示
OK,那就实现,问题都不大,简单的servlet就能实现,这里只展示核心代码和演示效果。简单写一个实现页面(代码十分潦草,原因是中间debug改了好多)
-
首先是对象定义:
@Data
@ExcelTarget("users")
public class Route{
@Excel(name="ID", type = 10)
int id;
@Excel(name="姓名", type = 1, width = 20)
String name;
@Excel(name="年龄", type = 10)
int age;
public Route(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Route() {
}
@Override
public String toString() {
return "Route{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
-
excel导出大批量数据(>200W)至数据库功能实现
ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
//直接就写死了
List<Route> list = ExcelImportUtil.importExcel(
new FileInputStream("E:/桌面/测试.xlsx"),
Route.class, params);
for(Route route : list){
int id = route.getId();
String name = route.getName();
int age = route.getAge();
//数据库插入函数
showDao.insertUser(id, name, age);
}
-
数据库导出大批量(>200W)数据至excel的功能
Workbook sheets = new HSSFWorkbook();
//获取表中数据量
int count = showDao.countUser();
for(int i = 0; i < count; i = i + 5000){
//获取数据库中数据,一次获取5000条
List<Route> routeList = showDao.findUser(i, (i + 5000 >= count ? count - i : 5000));
sheets = ExcelExportUtil.exportBigExcel(new ExportParams("用户信息列表", "用户信息"), Route.class, routeList);
}
//写入xls文件中
FileOutputStream fileOutputStream = new FileOutputStream("E:/桌面/renwu.xlsx");
sheets.write(fileOutputStream);
//关闭流
fileOutputStream.close();
sheets.close();
-
前端展示效果
应为每次不能查询太多数据,所以需要使用分页查询
OK,简单介绍到这,poi技术是开发中常用的技术,而easypoi正如它的名字那样非常简单,从学习到上手不超过一个小时即可