<!-- hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
<!-- EasyExcel依懒-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.5</version>
</dependency>
/**
* 任务导入 使用hutool工具类导入Excel文件
* @return
*/
@PostMapping("/import")
@ApiOperation("用户任务-任务导入")
public Result fileUpload(
@RequestParam("file") MultipartFile file
) {
try {
//使用hutool工具类导入Excel文件
ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
//读取excel中的数据,与User实体类一一对应
List<User> listData = reader.readAll(User.class);
//批量存入数据库中
//userTaskService.saveImportTask(listData);
} catch (IOException e) {
e.printStackTrace();
}
return new Result<>();
}
/**
* 统计导出 文件下载
*
* @return
*/
@GetMapping(value = "/export")
@ApiOperation(value = "统计导出")
public void statisticsExport(
@ApiParam(name = "province", value = "省")
@RequestParam("province") String province,
@ApiParam(name = "city", value = "市")
@RequestParam("city") String city,
@ApiParam(name = "counter", value = "区")
@RequestParam("counter") String counter,
@ApiParam(name = "startTime", value = "开始时间")
@RequestParam("startTime") Long startTime,
@ApiParam(name = "endTime", value = "结束时间")
@RequestParam("endTime") Long endTime,
@ApiParam(name = "orderByType", value = "排序类型 ASC 升序 DESC 倒序 默认倒序")
@RequestParam("orderByType") String orderByType,
HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("统计", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
FastDateFormat fastDateFormat = FastDateFormat.getInstance(DatePattern.NORM_DATETIME_PATTERN, TimeZone.getTimeZone("Asia/Shanghai"));
String startTimeStr = fastDateFormat.format(DateUtil.beginOfDay(new DateTime(startTime, TimeZone.getTimeZone("Asia/Shanghai"))));
String endTimeStr = fastDateFormat.format(DateUtil.endOfDay(new DateTime(endTime, TimeZone.getTimeZone("Asia/Shanghai"))));
// DemoDTO 查询条件入参
DemoDto dto= new DemoDto();
dto.setProvince(province);
dto.setCity(city);
dto.setCounter(counter);
dto.setStartTime(startTimeStr);
dto.setEndTime(endTimeStr);
dto.setOrderByType(orderByType);
//根据查询条件查询数据库---把需要导出的数据放到list中
List<DemoVO> list = task.findStatisByParams(DemoDto);
// 这里需要设置不关闭流
String dateTitle = "时间段:" + fastDateFormat.format(new DateTime(startTime,TimeZone.getTimeZone("Asia/Shanghai"))) + "至" + DateUtil.formatDate(new DateTime(endTime, TimeZone.getTimeZone("Asia/Shanghai")));
String rangeTitle = "范围:" +
(StrUtil.isBlank(province) ? "全部" : province) + "/" +
(StrUtil.isBlank(city) ? "全部" : city) + "/" +
(StrUtil.isBlank(counter) ? "全部" : counter);
EasyExcel.write(response.getOutputStream(), ClientDetailStatisVO.class)
.head(ClientDetailStatisVO.head(dateTitle, rangeTitle))
.autoCloseStream(Boolean.FALSE).sheet("统计")
//上面从数据库查出来的数据
.doWrite(statisByParams);
} catch (Exception e) {
// 重置response
response.reset();
throw new CustomException(Result.Status.INVALID_PARAM);
}
}