Merge branch 'dev/dev_xp' into dev/dev

This commit is contained in:
2025-01-24 14:28:54 +08:00

View File

@@ -34,7 +34,6 @@ import java.io.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.file.Files;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
@@ -227,45 +226,6 @@ public class ConvenientInquiryServiceImpl extends ServiceImpl<QuestionnaireMappe
return questionnaireVOS; return questionnaireVOS;
} }
// 输出为pdf 需要自己组装pdf内容
// 解决iText不显示中文问题
/*public static void main(String[] args) {
try {
// 创建文档
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();
// 加载字体文件
InputStream inputStream = ConvenientInquiryServiceImpl.class.getResourceAsStream("/font/msyhl.ttc");
byte[] fontBytes = toByteArray(inputStream);
inputStream.close();
// 创建BaseFont和Font对象
BaseFont baseFont = BaseFont.createFont("msyhl.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, fontBytes, null);
Font yaHeiFont = new Font(baseFont, 12, Font.NORMAL);
// 添加带有中文字体的段落
document.add(new Paragraph("你好,世界!", yaHeiFont));
document.close();
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
}
// 将InputStream转换为字节数组
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}*/
/** /**
* 近期新增用户 * 近期新增用户
*/ */
@@ -688,118 +648,101 @@ 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); // 创建标题行
String[] title = new String[]{"id", "payer", "platform", "payerTotal", "type", "status", XSSFRow titleRow = sheet.createRow(0);
"country", "city", "paymentMethod", "last4", "createTime"}; String[] title = new String[]{"id", "payer", "platform", "payerTotal", "type", "status",
//设置标题字体样式 "country", "city", "paymentMethod", "last4", "createTime"};
XSSFCellStyle cellStyle = workBook.createCellStyle(); //设置标题字体样式
XSSFFont font = workBook.createFont(); XSSFCellStyle cellStyle = workBook.createCellStyle();
font.setBold(true);//加粗 XSSFFont font = workBook.createFont();
font.setFontHeightInPoints((short) 14);//设置字体大小 font.setBold(true);//加粗
cellStyle.setFont(font); font.setFontHeightInPoints((short) 14);//设置字体大小
//设置标题列 cellStyle.setFont(font);
for (int i = 0; i < title.length; i++) { //设置标题列
//创建标题的单元格 for (int i = 0; i < title.length; i++) {
XSSFCell titleCell = titleRow.createCell(i); //创建标题的单元格
//填充标题数值 XSSFCell titleCell = titleRow.createCell(i);
titleCell.setCellValue(title[i]); //填充标题数值
//设置样式 titleCell.setCellValue(title[i]);
titleCell.setCellStyle(cellStyle); //设置样式
} titleCell.setCellStyle(cellStyle);
//填充数据 }
//第一行是标题所以要从第二行开始
for (int i = 0; i < paymentInfoVOS.size(); i++) { // 填充数据
PaymentInfoVO paymentInfoVO = paymentInfoVOS.get(i); //第一行是标题所以要从第二行开始
XSSFRow row = sheet.createRow(i + 1); for (int i = 0; i < paymentInfoVOS.size(); i++) {
for (int j = 0; j < title.length; j++) { PaymentInfoVO paymentInfoVO = paymentInfoVOS.get(i);
XSSFCell titleCell = row.createCell(j); XSSFRow row = sheet.createRow(i + 1);
String exportKey = title[j]; for (int j = 0; j < title.length; j++) {
switch (exportKey) { XSSFCell cell = row.createCell(j);
case "id": String exportKey = title[j];
titleCell.setCellValue(paymentInfoVO.getId()); switch (exportKey) {
break; case "id":
case "payer": cell.setCellValue(paymentInfoVO.getId());
titleCell.setCellValue(paymentInfoVO.getPayer()); break;
break; case "payer":
case "platform": cell.setCellValue(paymentInfoVO.getPayer());
titleCell.setCellValue(paymentInfoVO.getPlatform()); break;
break; case "platform":
case "payerTotal": cell.setCellValue(paymentInfoVO.getPlatform());
titleCell.setCellValue(paymentInfoVO.getPayerTotal()); break;
break; case "payerTotal":
case "type": cell.setCellValue(paymentInfoVO.getPayerTotal());
titleCell.setCellValue(paymentInfoVO.getType()); break;
break; case "type":
case "status": cell.setCellValue(paymentInfoVO.getType());
titleCell.setCellValue(paymentInfoVO.getStatus()); break;
break; case "status":
case "country": cell.setCellValue(paymentInfoVO.getStatus());
titleCell.setCellValue(paymentInfoVO.getCountry()); break;
break; case "country":
case "city": cell.setCellValue(paymentInfoVO.getCountry());
titleCell.setCellValue(paymentInfoVO.getCity()); break;
break; case "city":
case "paymentMethod": cell.setCellValue(paymentInfoVO.getCity());
titleCell.setCellValue(paymentInfoVO.getPaymentMethod()); break;
break; case "paymentMethod":
case "last4": cell.setCellValue(paymentInfoVO.getPaymentMethod());
titleCell.setCellValue(paymentInfoVO.getLast4()); break;
break; case "last4":
case "createTime": cell.setCellValue(paymentInfoVO.getLast4());
titleCell.setCellValue(paymentInfoVO.getCreateTime()); break;
break; case "createTime":
cell.setCellValue(paymentInfoVO.getCreateTime());
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转换为字节数组
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
// 读取文件并写入响应输出流 workBook.write(byteArrayOutputStream);
OutputStream responseOut = response.getOutputStream(); byte[] bytes = byteArrayOutputStream.toByteArray();
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()) { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
boolean delete = file.delete(); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
if (delete){
log.info("临时文件:{} 删除成功", fileName); // 将字节数组写入响应流
}else { try (OutputStream responseOut = response.getOutputStream()) {
log.info("临时文件:{} 删除失败", fileName); responseOut.write(bytes);
responseOut.flush();
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.info(e.getMessage()); log.info("文件下载失败:{}", e.getMessage());
throw new BusinessException("文件下载失败"); throw new BusinessException("文件下载失败");
} }
} }