流水修改

jieling
PC-202302260912\Administrator 2023-09-13 10:57:17 +08:00
parent 88dff608d0
commit 917020b609
2 changed files with 31 additions and 46 deletions

View File

@ -107,11 +107,6 @@
<artifactId>libthrift</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
@ -120,12 +115,12 @@
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
<version>3.17</version>
</dependency>
</dependencies>

View File

@ -1,7 +1,5 @@
package com.jmfy.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jmfy.WebSecurityConfig;
import com.jmfy.dao.*;
import com.jmfy.model.*;
@ -10,10 +8,10 @@ import com.jmfy.model.vo.OrderExcelVo;
import com.jmfy.redisProperties.RedisUserKey;
import com.jmfy.utils.*;
import com.jmfy.utils.DateUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -31,9 +29,6 @@ import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
@ -316,53 +311,48 @@ public class OrderInfoController {
long start = DateUtil.now();
LOGGER.info("开始导出订单写入文件name{},订单数:{}",name, cgPayOrders.size());
try (FileOutputStream fOut = new FileOutputStream(getExcelFilePath(name, start))) {
try (Workbook workbook = new XSSFWorkbook()) {
CellStyle contextStyle = workbook.createCellStyle();
int rowIndex = 1, cellIndex = 0;
Field[] fields = cgPayOrders.get(0).getClass().getDeclaredFields();
Sheet sheet = workbook.createSheet();
Row headerRow = sheet.createRow(0);
while (cellIndex < fields.length) {
Cell cell = headerRow.createCell(cellIndex);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(fields[cellIndex].getAnnotation(Fields.class).value());
cellIndex++;
}
SXSSFWorkbook workbook = new SXSSFWorkbook(); // 使用SXSSFWorkbook代替XSSFWorkbook
for (Corder cgPayOrder : cgPayOrders) {
if (rowIndex > 65535) {
// 超过行数限制,创建新的工作表
sheet = workbook.createSheet();
headerRow = sheet.createRow(0);
cellIndex = 0;
while (cellIndex < fields.length) {
Cell cell = headerRow.createCell(cellIndex);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(fields[cellIndex].getAnnotation(Fields.class).value());
cellIndex++;
}
rowIndex = 1;
}
CellStyle contextStyle = workbook.createCellStyle();
int rowIndex = 1, cellIndex = 0;
Field[] fields = cgPayOrders.get(0).getClass().getDeclaredFields();
SXSSFSheet sheet = workbook.createSheet();
Row headerRow = sheet.createRow(0);
while (cellIndex < fields.length) {
Cell cell = headerRow.createCell(cellIndex);
cell.setCellType(CellType.STRING);
cell.setCellValue(fields[cellIndex].getAnnotation(Fields.class).value());
cellIndex++;
}
int batchSize = 1000; // 每批写入的数据量
int batchCount = (int) Math.ceil((double) cgPayOrders.size() / batchSize);
for (int batch = 0; batch < batchCount; batch++) {
int startIndex = batch * batchSize;
int endIndex = Math.min(startIndex + batchSize, cgPayOrders.size());
for (int i = startIndex; i < endIndex; i++) {
Corder cgPayOrder = cgPayOrders.get(i);
cellIndex = 0;
Row currentRow = sheet.createRow(rowIndex);
while (cellIndex < fields.length) {
Cell cell = currentRow.createCell(cellIndex);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellType(CellType.STRING);
Object value = getFieldValueByName(fields[cellIndex].getName(), cgPayOrder);
setCellValue(cell, value, contextStyle);
cellIndex++;
}
rowIndex++;
}
workbook.write(fOut);
}catch (Exception e){
LOGGER.error("导出订单报错1{}", e.getMessage(), e);
sheet.flushRows(); // 刷新行数据到磁盘,释放内存
}
workbook.write(fOut);
workbook.dispose(); // 释放临时文件资源
LOGGER.info("导出订单操作excel文件写入耗时{}毫秒", DateUtil.now() - start);
} catch (Exception e) {
LOGGER.error("导出订单报错2{}", e.getMessage(), e);
LOGGER.error("导出订单报错{}", e.getMessage(), e);
}
LOGGER.info("导出订单操作excel文件写入耗时{}毫秒", DateUtil.now() - start);
}
private static String getExcelFilePath(String name, long start) throws Exception {