From d5a70a41029a18d4510082ee4badc2289869d2e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Mon, 24 Apr 2023 08:41:37 +0800 Subject: [PATCH] 'commit' --- src/main/java/UnitTest/TestMergeExcel.java | 7 ++- .../FengHuang/Util/ExcelCommonUtil.java | 15 +++--- .../FengHuang/Util/ExcelExportUtil.java | 9 +++- .../FengHuang/Yp/Controller/YpController.java | 44 +++++++++------ ...Excel.json => YangPuZhaoShengExcel_1.json} | 4 +- .../YangPuZhaoShengExcel_2.json | 54 +++++++++++++++++++ .../YangPuZhaoShengExcel_3.json | 54 +++++++++++++++++++ .../YangPuZhaoShengExcel_4.json | 54 +++++++++++++++++++ 8 files changed, 209 insertions(+), 32 deletions(-) rename src/main/resource/ExcelExportTemplate/{YangPuZhaoShengExcel.json => YangPuZhaoShengExcel_1.json} (90%) create mode 100644 src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_2.json create mode 100644 src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_3.json create mode 100644 src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_4.json diff --git a/src/main/java/UnitTest/TestMergeExcel.java b/src/main/java/UnitTest/TestMergeExcel.java index b627ff6..31798e6 100644 --- a/src/main/java/UnitTest/TestMergeExcel.java +++ b/src/main/java/UnitTest/TestMergeExcel.java @@ -7,13 +7,12 @@ import java.util.ArrayList; public class TestMergeExcel { public static void main(String[] args) { //这里是xls文件 - String[] filePaths = {"c:/Test/7840.xlsx","c:/Test/8210.xlsx"}; + String[] filePaths = {"c:/Test/7840.xlsx", "c:/Test/8210.xlsx"}; ArrayList list = new ArrayList<>(); for (String path : filePaths) { list.add(path); } - String path = "c:/Test"; - String fileName = "Result.xlsx"; - ExcelCommonUtil.mergeExcel(list, path, fileName); + String fileName = "c:/Test/Result.xlsx"; + ExcelCommonUtil.mergeExcel(list, fileName); } } diff --git a/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java b/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java index d6d97e3..366431b 100644 --- a/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java +++ b/src/main/java/com/dsideal/FengHuang/Util/ExcelCommonUtil.java @@ -848,10 +848,9 @@ public class ExcelCommonUtil { * #合并多个excel文件 * * @param fileLists excel文件路径 - * @param path 目标文件保存目录 * @param fileName 目标文件名称 */ - public static void mergeExcel(List fileLists, String path, String fileName) { + public static void mergeExcel(List fileLists, String fileName) { // 创建新的excel工作簿 XSSFWorkbook newExcelWorkBook = new XSSFWorkbook(); // 遍历需要合并的excel文件 @@ -884,15 +883,15 @@ public class ExcelCommonUtil { if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) { fileName += ".xlsx"; } - String excelFileName = path + File.separator + fileName; + // 判断文件是否存在 - File excelFile = new File(excelFileName); + File excelFile = new File(fileName); if (excelFile.exists()) { // 存在则删除 excelFile.delete(); } // 使用输出流写出 - try (FileOutputStream fos = new FileOutputStream(excelFileName)) { + try (FileOutputStream fos = new FileOutputStream(fileName)) { newExcelWorkBook.write(fos); fos.flush(); } catch (IOException e) { @@ -904,7 +903,6 @@ public class ExcelCommonUtil { e.printStackTrace(); } } - System.out.println("excel文件合并成功,合并后文件路径:" + excelFileName); } /** @@ -942,7 +940,7 @@ public class ExcelCommonUtil { */ private static void mergeSheetAllRegion(XSSFSheet tmpSheet, XSSFSheet newExcelSheet) { int num = tmpSheet.getNumMergedRegions(); - CellRangeAddress cellRange = null; + CellRangeAddress cellRange; for (int i = 0; i < num; i++) { cellRange = tmpSheet.getMergedRegion(i); newExcelSheet.addMergedRegion(cellRange); @@ -1005,7 +1003,6 @@ public class ExcelCommonUtil { newExcelCell.setCellErrorValue(tmpCell.getErrorCellValue()); } else if (tmpCellType == CellType.FORMULA) { newExcelCell.setCellFormula(tmpCell.getCellFormula()); - } else { - } + } } } diff --git a/src/main/java/com/dsideal/FengHuang/Util/ExcelExportUtil.java b/src/main/java/com/dsideal/FengHuang/Util/ExcelExportUtil.java index 0b42cc2..d0923db 100644 --- a/src/main/java/com/dsideal/FengHuang/Util/ExcelExportUtil.java +++ b/src/main/java/com/dsideal/FengHuang/Util/ExcelExportUtil.java @@ -3,6 +3,7 @@ package com.dsideal.FengHuang.Util; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.aspose.cells.License; +import com.jfinal.kit.StrKit; import com.jfinal.plugin.activerecord.Page; import com.jfinal.plugin.activerecord.Record; import org.apache.poi.hssf.usermodel.HSSFCell; @@ -28,9 +29,9 @@ public class ExcelExportUtil { * @param page * @param jo */ - public static void export(Page page, JSONObject jo, String fileName) { + public static void export(Page page, JSONObject jo, String fileName, String title) { //标题 - String title = jo.getString("title"); + if (StrKit.isBlank(title)) title = jo.getString("title"); //是不是显示序号 String showNumber = jo.getString("showNumber"); @@ -158,6 +159,10 @@ public class ExcelExportUtil { } } + public static void export(Page page, JSONObject jo, String fileName) { + export(page, jo, fileName); + } + public static HSSFWorkbook export(HSSFWorkbook hssfWorkbook, Page page, JSONObject jo) { //标题 diff --git a/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java b/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java index 3994914..5e30a10 100644 --- a/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java +++ b/src/main/java/com/dsideal/FengHuang/Yp/Controller/YpController.java @@ -3,10 +3,7 @@ package com.dsideal.FengHuang.Yp.Controller; import com.alibaba.fastjson.JSONObject; import com.dsideal.FengHuang.Interceptor.EmptyInterface; import com.dsideal.FengHuang.Interceptor.IsNumericInterface; -import com.dsideal.FengHuang.Util.Base64Util; -import com.dsideal.FengHuang.Util.CommonUtil; -import com.dsideal.FengHuang.Util.ExcelExportUtil; -import com.dsideal.FengHuang.Util.FileUtil; +import com.dsideal.FengHuang.Util.*; import com.dsideal.FengHuang.Yp.Model.YpModel; import com.jfinal.aop.Before; import com.jfinal.core.Controller; @@ -20,6 +17,7 @@ import com.jfinal.plugin.activerecord.Record; import com.jfinal.upload.UploadFile; import java.io.File; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -177,7 +175,7 @@ public class YpController extends Controller { */ @Before(POST.class) @EmptyInterface({"code", "name", "xb", "sfzh", "address", "father_name", "mother_name", "tel", "uuid"}) - public void save(String code, int task_id, String name, String xb, int bx_id, String address, String father_name, String mother_name, String sfzh, String tel, String uuid) { + public void save(String code, int task_id, String name, String xb, int bx_id, String address, String father_name, String mother_name, String sfzh, String tel, String uuid) { Kv kv = Kv.create(); boolean r = validateCaptcha(code); if (!r) { @@ -222,22 +220,38 @@ public class YpController extends Controller { * 功能:导出excel * * @param task_id - * @param bx_id 0:全部,否则指定的班型ID */ @Before(GET.class) - @IsNumericInterface({"task_id", "page", "limit"}) - public void exportExcel(int task_id, int bx_id) { + @IsNumericInterface({"task_id"}) + public void exportExcel(int task_id) { + //此次任务有哪些班型 + List list = model.getBx(task_id); + + String task_name = model.getTask(task_id).getStr("task_name"); + //需要合并的excel文件路径 + ArrayList mergeList = new ArrayList<>(); //模板文件 String excelPath = PathKit.getRootClassPath() + PropKit.get("excelExportTemplatePathSuffix").replace("\\", "/"); - String filePath = excelPath + "YangPuZhaoShengExcel.json"; - //转成 json对象 - JSONObject jo = FileUtil.readJsonFile(filePath); - //导出 - Page rs = model.getTaskInfo(task_id, bx_id, 1, 99999); + //按班型逐个生成 + for (int i = 0; i < list.size(); i++) { + Record record = list.get(i); + int bx_id = record.getInt("bx_id"); + String bx_name = record.getStr("bx_name"); + + String filePath = excelPath + "YangPuZhaoShengExcel_" + bx_id + ".json"; + //转成 json对象 + JSONObject jo = FileUtil.readJsonFile(filePath); + //导出 + Page rs = model.getTaskInfo(task_id, bx_id, 1, 99999); + String excelFile = excelPath + "excelTemp/" + UUID.randomUUID().toString().toUpperCase() + ".xls"; + ExcelExportUtil.export(rs, jo, excelFile, task_name + "申报结果(" + bx_name + ")"); + mergeList.add(excelFile); + } String excelFile = excelPath + "excelTemp/" + UUID.randomUUID().toString().toUpperCase() + ".xls"; - ExcelExportUtil.export(rs, jo, excelFile); + ExcelCommonUtil.mergeExcel(mergeList, excelFile); + //提供下载 - String filename = "申报结果.xls"; + String filename = task_name + "申报结果.xls"; renderFile(new File(excelFile), filename); } diff --git a/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel.json b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_1.json similarity index 90% rename from src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel.json rename to src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_1.json index a02c6d5..e0dae36 100644 --- a/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel.json +++ b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_1.json @@ -1,6 +1,6 @@ { - "title": "招生结果", - "sheetName": "结果", + "title": "托班招生结果", + "sheetName": "托班", "titleHeight": 30, "rowHeight": 30, "showNumber": true, diff --git a/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_2.json b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_2.json new file mode 100644 index 0000000..40eaa65 --- /dev/null +++ b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_2.json @@ -0,0 +1,54 @@ +{ + "title": "小班招生结果", + "sheetName": "小班", + "titleHeight": 30, + "rowHeight": 30, + "showNumber": true, + "colInfo": [ + { + "show_column_name": "申报班型", + "list_column_name": "bx_name", + "width": 40 + }, + { + "show_column_name": "姓名", + "list_column_name": "name", + "width": 40 + }, + { + "show_column_name": "性别", + "list_column_name": "xb", + "width": 20 + }, + { + "show_column_name": "家庭住址", + "list_column_name": "address", + "width": 50 + }, + { + "show_column_name": "父亲姓名", + "list_column_name": "address", + "width": 40 + }, + { + "show_column_name": "母亲姓名", + "list_column_name": "address", + "width": 40 + }, + { + "show_column_name": "身份证号", + "list_column_name": "sfzh", + "width": 40 + }, + { + "show_column_name": "联系电话", + "list_column_name": "tel", + "width": 40 + }, + { + "show_column_name": "申报时间", + "list_column_name": "create_time", + "width": 40 + } + ] +} \ No newline at end of file diff --git a/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_3.json b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_3.json new file mode 100644 index 0000000..0c34ccf --- /dev/null +++ b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_3.json @@ -0,0 +1,54 @@ +{ + "title": "中班招生结果", + "sheetName": "中班", + "titleHeight": 30, + "rowHeight": 30, + "showNumber": true, + "colInfo": [ + { + "show_column_name": "申报班型", + "list_column_name": "bx_name", + "width": 40 + }, + { + "show_column_name": "姓名", + "list_column_name": "name", + "width": 40 + }, + { + "show_column_name": "性别", + "list_column_name": "xb", + "width": 20 + }, + { + "show_column_name": "家庭住址", + "list_column_name": "address", + "width": 50 + }, + { + "show_column_name": "父亲姓名", + "list_column_name": "address", + "width": 40 + }, + { + "show_column_name": "母亲姓名", + "list_column_name": "address", + "width": 40 + }, + { + "show_column_name": "身份证号", + "list_column_name": "sfzh", + "width": 40 + }, + { + "show_column_name": "联系电话", + "list_column_name": "tel", + "width": 40 + }, + { + "show_column_name": "申报时间", + "list_column_name": "create_time", + "width": 40 + } + ] +} \ No newline at end of file diff --git a/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_4.json b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_4.json new file mode 100644 index 0000000..8d7e44d --- /dev/null +++ b/src/main/resource/ExcelExportTemplate/YangPuZhaoShengExcel_4.json @@ -0,0 +1,54 @@ +{ + "title": "大班招生结果", + "sheetName": "大班", + "titleHeight": 30, + "rowHeight": 30, + "showNumber": true, + "colInfo": [ + { + "show_column_name": "申报班型", + "list_column_name": "bx_name", + "width": 40 + }, + { + "show_column_name": "姓名", + "list_column_name": "name", + "width": 40 + }, + { + "show_column_name": "性别", + "list_column_name": "xb", + "width": 20 + }, + { + "show_column_name": "家庭住址", + "list_column_name": "address", + "width": 50 + }, + { + "show_column_name": "父亲姓名", + "list_column_name": "address", + "width": 40 + }, + { + "show_column_name": "母亲姓名", + "list_column_name": "address", + "width": 40 + }, + { + "show_column_name": "身份证号", + "list_column_name": "sfzh", + "width": 40 + }, + { + "show_column_name": "联系电话", + "list_column_name": "tel", + "width": 40 + }, + { + "show_column_name": "申报时间", + "list_column_name": "create_time", + "width": 40 + } + ] +} \ No newline at end of file