Browse Source

导出

master
shenzhouyu 6 months ago
parent
commit
0093c28c07
  1. 10
      src/main/java/com/xujie/sys/modules/pms/controller/QcReportController.java
  2. 63
      src/main/java/com/xujie/sys/modules/pms/service/Impl/QcReportServiceImpl.java
  3. 2
      src/main/java/com/xujie/sys/modules/pms/service/QcReportService.java

10
src/main/java/com/xujie/sys/modules/pms/controller/QcReportController.java

@ -76,4 +76,14 @@ public class QcReportController {
} }
} }
@PostMapping("/downloadQcRecordMillion")
public R downloadQcRecordMillion(HttpServletResponse response, @RequestBody QcReportData data)throws Exception {
try {
qcReportService.downloadQcRecordMillion(response, data);
return R.ok();
} catch (IOException e) {
return R.error(e.getMessage());
}
}
} }

63
src/main/java/com/xujie/sys/modules/pms/service/Impl/QcReportServiceImpl.java

@ -2,6 +2,7 @@ package com.xujie.sys.modules.pms.service.Impl;
import cn.idev.excel.FastExcel; import cn.idev.excel.FastExcel;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -18,10 +19,12 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.*;
@Service @Service
public class QcReportServiceImpl implements QcReportService { public class QcReportServiceImpl implements QcReportService {
@ -118,6 +121,66 @@ public class QcReportServiceImpl implements QcReportService {
} }
} }
@Override
public void downloadQcRecordMillion(HttpServletResponse response, QcReportData data) throws Exception {
ServletOutputStream out = null;
try {
out = response.getOutputStream();
// 设置EXCEL名称
String fileName = URLEncoder.encode("QC报表", "UTF-8");
// 设置SHEET名称
WriteSheet sheet = new WriteSheet();
sheet.setSheetName("明细列表sheet1");
int totalRowCount = 0;
// 查询总数并封装相关变量
if ("ipqc".equals(data.getDownloadType())) {
IPage<QcReportData> list = this.qcReportMapper.getIPQCReportCount(new Page<QcReportData>(data.getPage(), data.getLimit()), data);
totalRowCount = (int)list.getTotal();
int pageSize = ExcelConstant.PER_WRITE_ROW_COUNT;
int writeCount = totalRowCount % pageSize == 0 ? (totalRowCount / pageSize) : (totalRowCount / pageSize + 1);
// 1. 循环外创建线程池
ExecutorService executor = Executors.newFixedThreadPool(Math.min(writeCount, 10));
List<FutureTask<List<QcReportIPQCData>>> futureTaskList = new ArrayList<>();
for (int i = 0; i < writeCount; i++) {
int pageNum = i + 1; // 子线程中使用的页码
FutureTask<List<QcReportIPQCData>> futureTask = new FutureTask<>(() -> {
PageHelper.startPage(pageNum, pageSize);
return this.qcReportMapper.downloadIPQCRecord(data);
});
executor.execute(futureTask);
futureTaskList.add(futureTask);
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.HOURS);
ExcelWriter writer = EasyExcel.write(out, QcReportIPQCData.class).build();
sheet = EasyExcel.writerSheet("检验单明细").build();
for (FutureTask<List<QcReportIPQCData>> task : futureTaskList) {
List<QcReportIPQCData> dataList = task.get(); // 获取单批数据
writer.write(dataList, sheet); // 分批写入
}
writer.finish();
}
// 下载EXCEL
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
out.flush();
} finally {
if (out != null) {
out.close();
}
}
}
private List<QcReportData> getIQCData(int writeCount, int pageSize, QcReportData data) { private List<QcReportData> getIQCData(int writeCount, int pageSize, QcReportData data) {
List<QcReportData> allData = new ArrayList<>(); List<QcReportData> allData = new ArrayList<>();
for (int i = 0; i < writeCount; i++) { for (int i = 0; i < writeCount; i++) {

2
src/main/java/com/xujie/sys/modules/pms/service/QcReportService.java

@ -16,4 +16,6 @@ public interface QcReportService {
PageUtils getOQCReport(QcReportData data); PageUtils getOQCReport(QcReportData data);
void downloadQcRecord(HttpServletResponse response, QcReportData data) throws IOException; void downloadQcRecord(HttpServletResponse response, QcReportData data) throws IOException;
void downloadQcRecordMillion(HttpServletResponse response, QcReportData data)throws Exception;
} }
Loading…
Cancel
Save