# Excel工具
# 引入工具包
<dependency>
<groupId>com.jinke.ts</groupId>
<artifactId>ts-module-excel</artifactId>
<version>2.5.9-RELEASE</version>
</dependency>
# 一、Excel导出
性能测试数据(测试环境) 测试条件: 测试环境数据库+容器服务 导出字段13个,包含1个字典组装字段
数据量 | 耗时 | 是否正常导出 |
---|---|---|
1w | 4S | 是 |
# 1. 新增下载缓存地址配置文件
默认临时目录为项目本地/excel目录
# 2. 实体类加入需要导出的字段
public class DemoEntity implements Serializable {
/**
* sort 导出时在excel中排序
* name 导出到Excel中的名字.
* dateFormat 日期格式, 如: yyyy-MM-dd
* dictType 如果是字典类型,请设置字典的type值 注意,加入字典后,处理逻辑会每条数据都要读取缓存并匹配字典值,开发人员需关注时效
* readConverterExp 读取内容转表达式 (如: 0=男,1=女,2=未知)
* separator 分隔符,读取字符串组内容
* scale BigDecimal 精度 默认:-1(默认不开启BigDecimal格式化)
* roundingMode BigDecimal 舍入规则 默认:BigDecimal.ROUND_HALF_EVEN
* cellType 导出类型(0数字 1字符串)
* height 导出时在excel中每个列的高度 单位为字符
* width 导出时在excel中每个列的宽 单位为字符
* suffix 文字后缀,如% 90 变成90%
* defaultValue 当值为空时,字段的默认值
* prompt 提示信息
* combo 设置只能选择不能输入的列内容.
* isExport 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
* targetAttr 另一个类中的属性名称,支持多级获取,以小数点隔开
* isStatistics 是否自动统计数据,在最后追加一行统计数据总和
* align 导出字段对齐方式(0:默认;1:靠左;2:居中;3:靠右)
* type 字段类型(0:导出导入;1:仅导出;2:仅导入)
*/
@Excel(name="用户编号")
private String userId;
}
# 3. 控制层代码示例
@PostMapping("/export")
@ResponseBody
public ResponseJson export() {
List<DemoEntity> list = demoService.query();
ExcelUtil<DemoEntity> util = new ExcelUtil<DemoEntity>(DemoEntity.class);
return util.exportExcel(list, "导出文件名");
}
# 4. 部分情况需要搭配通用下载函数
@GetMapping("excel/download")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
try {
if (!FileUtils.checkAllowDownload(fileName)) {
throw new Exception("文件名称({})非法,不允许下载。 ");
}
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
String filePath = ".." + fileName;
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
FileUtils.setAttachmentResponseHeader(response, realFileName);
FileUtils.writeBytes(filePath, response.getOutputStream());
if (delete) {
FileUtils.deleteFile(filePath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
# 二、 Excel导入
# 1. 默认从excel第一行作为表头开始读取数据,
importExcel(InputStream is)
@PostMapping("/importData")
@ResponseBody
public ResponseJson importData(MultipartFile file, boolean updateSupport) throws Exception
{
ExcelUtil<DemoEntity> util = new ExcelUtil<DemoEntity>(DemoEntity.class);
List<DemoEntity> userList = util.importExcel(file.getInputStream());
//业务代码...
return new ResponseJson(200,message);
}
# 2.从excel第N行作为表头开始读取数据,有些特殊导入模板开头几行是模板说明,真正的表头和数据是从第N行开始,
- 因此,新增了参数
startRowNum
的同名方法:importExcel(InputStream is, int startRowNum)
。 startRowNum
需要从0开始算,比如startRowNum=2
时,指的是从excel第3行开始解析
@PostMapping("/importData")
@ResponseBody
public ResponseJson importData(MultipartFile file, boolean updateSupport) throws Exception
{
ExcelUtil<DemoEntity> util = new ExcelUtil<DemoEntity>(DemoEntity.class);
List<DemoEntity> list = null;
try {
// 这里的2表示从excel第3行作为表头开始解析数据
list = util.importExcel(file.getInputStream(),2);
} catch (Exception e) {
throw new AppException("导入数据解析出现异常,请检查文件格式以及表格是否存在删减!");
}
//业务代码...
return new ResponseJson(200,message);
}