对poi进行了封装,简化代码量,提高开发效率,避免出现大数据量下效率低甚至可能出现内存溢出的情况
仓库地址:https://gitee.com/lemur/easypoi
maven依赖:
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.3.0</version>
</dependency>
使用:
1.对需要用到的实体类字段添加注解@Excel
width设置宽度
日期可设置格式
2.导出
HSSF 03版本Excel .xls
XSSF 07版本Excel .xlsx
@ApiOperation("导出数据")
@GetMapping(value = "/export",produces = "application/octet-stream")
public void exportEmp(HttpServletResponse response){
//得到数据
List<Employee> employeeList = employeeService.list();
ExportParams params = new ExportParams("员工表", "员工表", ExcelType.HSSF);
Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, employeeList);
ServletOutputStream outputStream=null;
try {
//流形式
response.setHeader("content-type","application/octet-stream");
//防止中文乱码
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("员工表.xls","UTF-8"));
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.导入
@ApiOperation("导入数据")
@PostMapping("/import")
public ResultVo importEmp(MultipartFile file){
ImportParams params = new ImportParams();
//标题行
params.setTitleRows(1);
try {
List<Employee> list = ExcelImportUtil.importExcel(file.getInputStream(), Employee.class, params);
if (employeeService.saveBatch(list)){
return ResultVo.success("导入成功");
}
} catch (Exception e) {
e.printStackTrace();
}
return ResultVo.error("导入失败");
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/4972.html