From 46f37942d25f3bd8458d61b526daebbc2bc172a7 Mon Sep 17 00:00:00 2001 From: xupei Date: Fri, 24 Jan 2025 13:26:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E4=BA=A4=E6=98=93=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E4=B8=BAexcel=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 + .../java/com/ai/da/common/utils/DateUtil.java | 1 + .../ConvenientInquiryController.java | 8 + .../da/service/ConvenientInquiryService.java | 3 + .../impl/ConvenientInquiryServiceImpl.java | 141 ++++++++++++++++++ 5 files changed, 158 insertions(+) diff --git a/pom.xml b/pom.xml index db7bdb0b..18d4c47e 100644 --- a/pom.xml +++ b/pom.xml @@ -315,6 +315,11 @@ google-http-client-jackson2 1.41.5 + + org.apache.poi + poi-ooxml + 5.2.3 + diff --git a/src/main/java/com/ai/da/common/utils/DateUtil.java b/src/main/java/com/ai/da/common/utils/DateUtil.java index e1b1d755..e386ad54 100644 --- a/src/main/java/com/ai/da/common/utils/DateUtil.java +++ b/src/main/java/com/ai/da/common/utils/DateUtil.java @@ -18,6 +18,7 @@ public class DateUtil { public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static final String YYYYMM = "yyyyMM"; public static final String YYYY_MM_DD = "yyyyMMdd"; + public static final String YYYY_MM_DD_hh_mm_ss = "yyyyMMddHHMMss"; /** * LocalDate -> Date diff --git a/src/main/java/com/ai/da/controller/ConvenientInquiryController.java b/src/main/java/com/ai/da/controller/ConvenientInquiryController.java index 0f8547d5..01709f12 100644 --- a/src/main/java/com/ai/da/controller/ConvenientInquiryController.java +++ b/src/main/java/com/ai/da/controller/ConvenientInquiryController.java @@ -26,6 +26,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Nullable; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.text.SimpleDateFormat; import java.util.*; @@ -208,4 +209,11 @@ public class ConvenientInquiryController { public Response>> getCities(){ return Response.success(convenientInquiryService.getCities()); } + + @ApiOperation("下载交易记录") + @PostMapping("/exportTransactionRecords") + public void exportTransactionRecords(@Valid @RequestBody QueryPaymentInfoDTO queryPaymentInfoDTO, HttpServletResponse response){ + convenientInquiryService.exportTransactionRecords(queryPaymentInfoDTO, response); +// return Response.success(); + } } diff --git a/src/main/java/com/ai/da/service/ConvenientInquiryService.java b/src/main/java/com/ai/da/service/ConvenientInquiryService.java index bb44b6b7..19ac9a26 100644 --- a/src/main/java/com/ai/da/service/ConvenientInquiryService.java +++ b/src/main/java/com/ai/da/service/ConvenientInquiryService.java @@ -13,6 +13,7 @@ import com.ai.da.model.vo.QueryUserConditionsVO; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; +import javax.servlet.http.HttpServletResponse; import java.util.List; import java.util.Map; @@ -50,4 +51,6 @@ public interface ConvenientInquiryService extends IService { PageBaseResponse queryTransactionRecords(QueryPaymentInfoDTO queryPaymentInfoDTO); Map> getCities(); + + void exportTransactionRecords(QueryPaymentInfoDTO queryPaymentInfoDTO, HttpServletResponse response); } diff --git a/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java b/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java index e0d6d978..46fa27f3 100644 --- a/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java @@ -6,6 +6,7 @@ import com.ai.da.common.context.UserContext; import com.ai.da.common.enums.CreditsEventsEnum; import com.ai.da.common.response.PageBaseResponse; import com.ai.da.common.utils.CopyUtil; +import com.ai.da.common.utils.DateUtil; import com.ai.da.mapper.primary.*; import com.ai.da.mapper.primary.entity.Account; import com.ai.da.mapper.primary.entity.Questionnaire; @@ -23,12 +24,17 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mysql.cj.util.StringUtils; import io.netty.util.internal.StringUtil; import lombok.extern.slf4j.Slf4j; +import org.apache.poi.xssf.usermodel.*; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.*; import java.math.BigDecimal; import java.math.RoundingMode; +import java.net.URLEncoder; +import java.nio.file.Files; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; @@ -663,4 +669,139 @@ public class ConvenientInquiryServiceImpl extends ServiceImpl paymentInfoVOS = paymentInfoMapper.queryPaymentInfo(queryPaymentInfoDTO.getPlatform(), queryPaymentInfoDTO.getPayerTotal(), + queryPaymentInfoDTO.getType(), queryPaymentInfoDTO.getStatus(), + queryPaymentInfoDTO.getCountry(), queryPaymentInfoDTO.getCity(), + queryPaymentInfoDTO.getStartTime(), queryPaymentInfoDTO.getEndTime(), + total.intValue(), 0, "ASC", queryPaymentInfoDTO.getPayer()); + excelExport(paymentInfoVOS, response); + } + + public void excelExport(List paymentInfoVOS, HttpServletResponse response) { + if (paymentInfoVOS == null || paymentInfoVOS.isEmpty()) { + log.info("无数据,直接返回,不生成空文件"); + return; + } + //生成文件路径 + String newFilePath = "files"; + //文件名称 + String time = DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD_hh_mm_ss); + String fileName = "paymentInfo_"+ time + ".xlsx"; + //输出流 + OutputStream out = null; + // 创建Excel文件 + XSSFWorkbook workBook = new XSSFWorkbook(); + // 创建Excel工作表(Table),之后的数据都添加到该表中 + XSSFSheet sheet = workBook.createSheet("数据导出"); + // 设置列的宽度 + sheet.setDefaultColumnWidth(16); + // 创建标题行 + XSSFRow titleRow = sheet.createRow(0); + String[] title = new String[]{"id", "payer", "platform", "payerTotal", "type", "status", + "country", "city", "paymentMethod", "last4", "createTime"}; + //设置标题字体样式 + XSSFCellStyle cellStyle = workBook.createCellStyle(); + XSSFFont font = workBook.createFont(); + font.setBold(true);//加粗 + font.setFontHeightInPoints((short) 14);//设置字体大小 + cellStyle.setFont(font); + //设置标题列 + for (int i = 0; i < title.length; i++) { + //创建标题的单元格 + XSSFCell titleCell = titleRow.createCell(i); + //填充标题数值 + titleCell.setCellValue(title[i]); + //设置样式 + titleCell.setCellStyle(cellStyle); + } + //填充数据 + //第一行是标题所以要从第二行开始 + for (int i = 0; i < paymentInfoVOS.size(); i++) { + PaymentInfoVO paymentInfoVO = paymentInfoVOS.get(i); + XSSFRow row = sheet.createRow(i + 1); + for (int j = 0; j < title.length; j++) { + XSSFCell titleCell = row.createCell(j); + String exportKey = title[j]; + switch (exportKey) { + case "id": + titleCell.setCellValue(paymentInfoVO.getId()); + break; + case "payer": + titleCell.setCellValue(paymentInfoVO.getPayer()); + break; + case "platform": + titleCell.setCellValue(paymentInfoVO.getPlatform()); + break; + case "payerTotal": + titleCell.setCellValue(paymentInfoVO.getPayerTotal()); + break; + case "type": + titleCell.setCellValue(paymentInfoVO.getType()); + break; + case "status": + titleCell.setCellValue(paymentInfoVO.getStatus()); + break; + case "country": + titleCell.setCellValue(paymentInfoVO.getCountry()); + break; + case "city": + titleCell.setCellValue(paymentInfoVO.getCity()); + break; + case "paymentMethod": + titleCell.setCellValue(paymentInfoVO.getPaymentMethod()); + break; + case "last4": + titleCell.setCellValue(paymentInfoVO.getLast4()); + break; + case "createTime": + titleCell.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"); + 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()) { + boolean delete = file.delete(); + if (delete){ + log.info("临时文件:{}, 删除成功", fileName); + }else { + log.info("临时文件:{}, 删除失败", fileName); + } + } + } catch (Exception e) { + log.info(e.getMessage()); + throw new BusinessException("文件下载失败"); + } + } + }