/*********************pom***************/
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.0-beta6</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<artifactId>commons-collections4</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
</exclusions>
</dependency>
/**************controller**********/
/** 导出**/
@PostMapping(value = "/token")
@ApiImplicitParams({@ApiImplicitParam(name = "ACCESS_TOKEN", value = "接口调用凭证", defaultValue = "06855244f2f221da4cd0a395c0d3c68c", dataType = "string", required = true, paramType = "query")})
public void getTOKENRecordExport(HttpServletResponse response, @ApiParam(value = "条件") @RequestBody InquireConditionParam param) {
List<DataTokenRecordDto> pageInfo = dataTokenRecordMapper.getTokenList(param);
String fileName = new String(new SimpleDateFormat("yyyy-MM-dd").format(new Date()).getBytes(), StandardCharsets.UTF_8);
try {
ExcelWriter excelWriter = ExcelUtils.writeExcel(response.getOutputStream(), ExcelTypeEnum.XLSX, true, pageInfo);
if (CollectionUtils.isEmpty(pageInfo)) {
fileName = "无数据";
}
response.setContentType("multipart/form-data");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=T享TOKEN列表-" + fileName + ".xlsx");
excelWriter.finish();
} catch (IOException e) {
e.printStackTrace();
}
}
@ApiOperation(value = "导入Excel数据", notes = "导入Excel数据")
@PostMapping("/import/excel/{cid}")
public void importHouseExcel(MultipartFile file, @ApiParam("小区id") @PathVariable("cid") Integer cid) {
try {
ExcelListener listener = new ExcelListener();
ExcelReader excelReader = ExcelUtils.readerExcel(file.getInputStream(), listener, HouseExcelParam.class);
houseService.importHouseExcel(listener.getData(), cid);
excelReader.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
@ApiOperation(value = "下载Excel模板", notes = "下载Excel模板")
@GetMapping("/download/excel")
public void downloadHouseExcel() {
try {
String path = getClass().getResource("/static/").getPath() + "house.xlsx";
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=house.xlsx");
ExcelUtils.downloadExcel(path, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
//*****************service****************/
@ApiOperation(value = "导入Excel数据", notes = "导入Excel数据")
@PostMapping("/import/excel/{cid}")
public void importHouseExcel(MultipartFile file, @ApiParam("小区id") @PathVariable("cid") Integer cid) {
try {
List<Map<String, String>> maps = ExcelUtils.parseExcel(file);
houseService.importHouseExcel(maps, cid);
} catch (Exception e) {
e.printStackTrace();
}
}
@ApiOperation(value = "下载Excel模板", notes = "下载Excel模板")
@GetMapping("/download/excel")
public void downloadHouseExcel() {
try {
String path = getClass().getResource("/static/").getPath() + "house.xlsx";
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=house.xlsx");
ExcelUtils.downloadExcel(path, response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
/*****************utls********************/
/**
* @author qcdl
*/
public final class ExcelUtils {
/**
* 导出
*
* @param out
* @param excelType
* @param needHead
* @param data
* @return
*/
public static ExcelWriter writeExcel(OutputStream out, ExcelTypeEnum excelType, boolean needHead, List<? extends BaseRowModel> data) {
ExcelWriter excelWriter = EasyExcelFactory.getWriter(out, excelType, needHead);
Sheet sheet = new Sheet(1, 0, data.get(0).getClass());
excelWriter.write(data, sheet);
return excelWriter;
}
/**
* 导入
*
* @param in
* @param listener
* @param clazz
* @return
*/
public static ExcelReader readerExcel(InputStream in, ExcelListener listener, Class<? extends BaseRowModel> clazz) {
ExcelReader excelReader = EasyExcelFactory.getReader(in, listener);
Sheet sheet = new Sheet(1, 1, clazz);
excelReader.read(sheet);
return excelReader;
}
}
/**
* 下载Excel
*
* @param path
* @param out
* @return
* @throws IOException
*/
public static void downloadExcel(String path, OutputStream out) throws IOException {
FileInputStream input = new FileInputStream(path);
byte[] bytes = new byte[2048];
int len;
while ((len = input.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
input.close();
}
网友评论