优化 导出交易记录文件
This commit is contained in:
@@ -688,19 +688,18 @@ public class ConvenientInquiryServiceImpl extends ServiceImpl<QuestionnaireMappe
|
|||||||
log.info("无数据,直接返回,不生成空文件");
|
log.info("无数据,直接返回,不生成空文件");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//生成文件路径
|
|
||||||
String newFilePath = "files";
|
// 文件名称
|
||||||
//文件名称
|
|
||||||
String time = DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD_hh_mm_ss);
|
String time = DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD_hh_mm_ss);
|
||||||
String fileName = "paymentInfo_"+ time + ".xlsx";
|
String fileName = "paymentInfo_" + time + ".xlsx";
|
||||||
//输出流
|
|
||||||
OutputStream out = null;
|
|
||||||
// 创建Excel文件
|
// 创建Excel文件
|
||||||
XSSFWorkbook workBook = new XSSFWorkbook();
|
try (XSSFWorkbook workBook = new XSSFWorkbook()) {
|
||||||
// 创建Excel工作表(Table),之后的数据都添加到该表中
|
// 创建Excel工作表(Table),之后的数据都添加到该表中
|
||||||
XSSFSheet sheet = workBook.createSheet("数据导出");
|
XSSFSheet sheet = workBook.createSheet("数据导出");
|
||||||
// 设置列的宽度
|
// 设置列宽
|
||||||
sheet.setDefaultColumnWidth(16);
|
sheet.setDefaultColumnWidth(16);
|
||||||
|
|
||||||
// 创建标题行
|
// 创建标题行
|
||||||
XSSFRow titleRow = sheet.createRow(0);
|
XSSFRow titleRow = sheet.createRow(0);
|
||||||
String[] title = new String[]{"id", "payer", "platform", "payerTotal", "type", "status",
|
String[] title = new String[]{"id", "payer", "platform", "payerTotal", "type", "status",
|
||||||
@@ -720,86 +719,70 @@ public class ConvenientInquiryServiceImpl extends ServiceImpl<QuestionnaireMappe
|
|||||||
//设置样式
|
//设置样式
|
||||||
titleCell.setCellStyle(cellStyle);
|
titleCell.setCellStyle(cellStyle);
|
||||||
}
|
}
|
||||||
//填充数据
|
|
||||||
|
// 填充数据
|
||||||
//第一行是标题所以要从第二行开始
|
//第一行是标题所以要从第二行开始
|
||||||
for (int i = 0; i < paymentInfoVOS.size(); i++) {
|
for (int i = 0; i < paymentInfoVOS.size(); i++) {
|
||||||
PaymentInfoVO paymentInfoVO = paymentInfoVOS.get(i);
|
PaymentInfoVO paymentInfoVO = paymentInfoVOS.get(i);
|
||||||
XSSFRow row = sheet.createRow(i + 1);
|
XSSFRow row = sheet.createRow(i + 1);
|
||||||
for (int j = 0; j < title.length; j++) {
|
for (int j = 0; j < title.length; j++) {
|
||||||
XSSFCell titleCell = row.createCell(j);
|
XSSFCell cell = row.createCell(j);
|
||||||
String exportKey = title[j];
|
String exportKey = title[j];
|
||||||
switch (exportKey) {
|
switch (exportKey) {
|
||||||
case "id":
|
case "id":
|
||||||
titleCell.setCellValue(paymentInfoVO.getId());
|
cell.setCellValue(paymentInfoVO.getId());
|
||||||
break;
|
break;
|
||||||
case "payer":
|
case "payer":
|
||||||
titleCell.setCellValue(paymentInfoVO.getPayer());
|
cell.setCellValue(paymentInfoVO.getPayer());
|
||||||
break;
|
break;
|
||||||
case "platform":
|
case "platform":
|
||||||
titleCell.setCellValue(paymentInfoVO.getPlatform());
|
cell.setCellValue(paymentInfoVO.getPlatform());
|
||||||
break;
|
break;
|
||||||
case "payerTotal":
|
case "payerTotal":
|
||||||
titleCell.setCellValue(paymentInfoVO.getPayerTotal());
|
cell.setCellValue(paymentInfoVO.getPayerTotal());
|
||||||
break;
|
break;
|
||||||
case "type":
|
case "type":
|
||||||
titleCell.setCellValue(paymentInfoVO.getType());
|
cell.setCellValue(paymentInfoVO.getType());
|
||||||
break;
|
break;
|
||||||
case "status":
|
case "status":
|
||||||
titleCell.setCellValue(paymentInfoVO.getStatus());
|
cell.setCellValue(paymentInfoVO.getStatus());
|
||||||
break;
|
break;
|
||||||
case "country":
|
case "country":
|
||||||
titleCell.setCellValue(paymentInfoVO.getCountry());
|
cell.setCellValue(paymentInfoVO.getCountry());
|
||||||
break;
|
break;
|
||||||
case "city":
|
case "city":
|
||||||
titleCell.setCellValue(paymentInfoVO.getCity());
|
cell.setCellValue(paymentInfoVO.getCity());
|
||||||
break;
|
break;
|
||||||
case "paymentMethod":
|
case "paymentMethod":
|
||||||
titleCell.setCellValue(paymentInfoVO.getPaymentMethod());
|
cell.setCellValue(paymentInfoVO.getPaymentMethod());
|
||||||
break;
|
break;
|
||||||
case "last4":
|
case "last4":
|
||||||
titleCell.setCellValue(paymentInfoVO.getLast4());
|
cell.setCellValue(paymentInfoVO.getLast4());
|
||||||
break;
|
break;
|
||||||
case "createTime":
|
case "createTime":
|
||||||
titleCell.setCellValue(paymentInfoVO.getCreateTime());
|
cell.setCellValue(paymentInfoVO.getCreateTime());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
File file = new File(newFilePath + File.separator + fileName);
|
|
||||||
out = Files.newOutputStream(file.toPath());
|
|
||||||
workBook.write(out);
|
|
||||||
out.flush();
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
// 将workBook转换为字节数组
|
||||||
|
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
|
||||||
|
workBook.write(byteArrayOutputStream);
|
||||||
|
byte[] bytes = byteArrayOutputStream.toByteArray();
|
||||||
|
|
||||||
|
// 设置响应头
|
||||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||||
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||||
// 读取文件并写入响应输出流
|
|
||||||
OutputStream responseOut = response.getOutputStream();
|
|
||||||
FileInputStream fileInputStream = new FileInputStream(newFilePath + File.separator + fileName);
|
|
||||||
// 将文件转成字节数组,再将数组写入响应的输出流
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int bytesRead = -1;
|
|
||||||
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
||||||
responseOut.write(buffer, 0, bytesRead);
|
|
||||||
}
|
|
||||||
// 刷新输出流
|
|
||||||
responseOut.flush();
|
|
||||||
// 关闭流
|
|
||||||
fileInputStream.close();
|
|
||||||
responseOut.close();
|
|
||||||
|
|
||||||
// 路径为文件且不为空则进行删除
|
// 将字节数组写入响应流
|
||||||
if (file.isFile() && file.exists()) {
|
try (OutputStream responseOut = response.getOutputStream()) {
|
||||||
boolean delete = file.delete();
|
responseOut.write(bytes);
|
||||||
if (delete){
|
responseOut.flush();
|
||||||
log.info("临时文件:{}, 删除成功", fileName);
|
|
||||||
}else {
|
|
||||||
log.info("临时文件:{}, 删除失败", fileName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.info(e.getMessage());
|
log.info("文件下载失败:{}", e.getMessage());
|
||||||
throw new BusinessException("文件下载失败");
|
throw new BusinessException("文件下载失败");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user