main
黄海 2 years ago
parent 741f4ce20d
commit d5a70a4102

@ -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<String> 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);
}
}

@ -848,10 +848,9 @@ public class ExcelCommonUtil {
* #excel
*
* @param fileLists excel
* @param path
* @param fileName
*/
public static void mergeExcel(List<String> fileLists, String path, String fileName) {
public static void mergeExcel(List<String> 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 {
}
}
}
}

@ -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) {
//标题

@ -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<Record> list = model.getBx(task_id);
String task_name = model.getTask(task_id).getStr("task_name");
//需要合并的excel文件路径
ArrayList<String> mergeList = new ArrayList<>();
//模板文件
String excelPath = PathKit.getRootClassPath() + PropKit.get("excelExportTemplatePathSuffix").replace("\\", "/");
String filePath = excelPath + "YangPuZhaoShengExcel.json";
//转成 json对象
JSONObject jo = FileUtil.readJsonFile(filePath);
//导出
Page<Record> 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<Record> 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);
}

@ -1,6 +1,6 @@
{
"title": "招生结果",
"sheetName": "结果",
"title": "托班招生结果",
"sheetName": "托班",
"titleHeight": 30,
"rowHeight": 30,
"showNumber": true,

@ -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
}
]
}

@ -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
}
]
}

@ -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
}
]
}
Loading…
Cancel
Save