diff --git a/WebRoot/Excel/0189510b-f70b-4a3c-a3cd-b91ee7641487.xlsx b/WebRoot/Excel/0189510b-f70b-4a3c-a3cd-b91ee7641487.xlsx new file mode 100644 index 00000000..5b0651f9 Binary files /dev/null and b/WebRoot/Excel/0189510b-f70b-4a3c-a3cd-b91ee7641487.xlsx differ diff --git a/WebRoot/Excel/5eda6518-9937-4698-b378-165f0bf029b3.xlsx b/WebRoot/Excel/5eda6518-9937-4698-b378-165f0bf029b3.xlsx new file mode 100644 index 00000000..4ee1f336 Binary files /dev/null and b/WebRoot/Excel/5eda6518-9937-4698-b378-165f0bf029b3.xlsx differ diff --git a/src/main/java/UnitTest/TestExportExcel.java b/src/main/java/UnitTest/TestExportExcel.java index a278a0dc..9059ee4e 100644 --- a/src/main/java/UnitTest/TestExportExcel.java +++ b/src/main/java/UnitTest/TestExportExcel.java @@ -18,17 +18,6 @@ import java.util.Map; public class TestExportExcel { - /** - * 功能:获取指定列的列描述信息 - * - * @param table_name - * @return - */ - public static List getTableStructInfo(String table_name) { - String sql = "select col.column_name, col.ordinal_position as o, d.description as column_description from information_schema.columns col join pg_class c on c.relname = col.table_name left join pg_description d on d.objoid = c.oid and d.objsubid = col.ordinal_position where col.table_schema = 'public' and table_name=? order by col.ordinal_position"; - return Db.find(sql, table_name); - } - public static void main(String[] args) throws IOException { //读取库 HikariCpPlugin hp = new HikariCpPlugin("jdbc:postgresql://10.10.14.71:5432/szjz_db", "postgres", @@ -39,122 +28,5 @@ public class TestExportExcel { arp.setDialect(new PostgreSqlDialect()); arp.start(); - int job_id = 47; - String sql = "select * from t_collect_job where job_id=?"; - Record record = Db.findFirst(sql, job_id); - String job_name = record.getStr("job_name"); - - int job_type = record.getInt("job_type");//任务类型:1:表单,2:EXCEL模板 - String table_name = record.getStr("form_table_name");//表格名称 - if (job_type == 1) { - Map _map = new HashMap<>(); - List listStruct = getTableStructInfo(table_name); - int idx = 0; - for (Record r : listStruct) { - String column_name = r.getStr("column_name"); - //准备第几列用哪一个字段 - if (column_name.equals("id")) continue; - if (column_name.equals("bureau_id")) continue; - if (column_name.equals("person_id")) continue; - if (column_name.equals("class_id")) continue; - if (column_name.equals("job_id")) continue; - _map.put(idx++, r); - } - //获取数据 - sql = "select * from " + table_name + " where job_id=?"; - List data = Db.find(sql, job_id); - //创建Excel - SXSSFWorkbook workbook = new SXSSFWorkbook();//默认100行,超100行将写入临时文件 - workbook.setCompressTempFiles(false); //是否压缩临时文件,否则写入速度更快,但更占磁盘,但程序最后是会将临时文件删掉的 - //开始生成EXCEL - String sheet_name = "填报情况"; - //获取数据 - Sheet sheet = workbook.createSheet(sheet_name); - // 创建单元格样式对象 - CellStyle headerStyle = workbook.createCellStyle(); - // 设置水平居中 - headerStyle.setAlignment(HorizontalAlignment.CENTER); - // 设置垂直居中 - headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); - // 设置边框样式 - headerStyle.setBorderTop(BorderStyle.THIN); - headerStyle.setBorderBottom(BorderStyle.THIN); - headerStyle.setBorderLeft(BorderStyle.THIN); - headerStyle.setBorderRight(BorderStyle.THIN); - - // 创建字体对象并设置字体颜色为白色 - Font font = workbook.createFont(); - font.setFontHeightInPoints((short) 14); - font.setBold(true); - font.setFontName("黑体"); - font.setColor(IndexedColors.BLACK.getIndex()); - // 设置背景色为浅蓝色 #d2f4f2 - //颜色 - String str = "#d2f4f2"; - String sr = str.substring(1, 3); - String sg = str.substring(3, 5); - String sb = str.substring(5, 7); - //16进制的字符串转为int - int r = Integer.parseInt(sr, 16); - int g = Integer.parseInt(sg, 16); - int b = Integer.parseInt(sb, 16); - XSSFColor rbg = new XSSFColor(new java.awt.Color(r, g, b), new DefaultIndexedColorMap()); - headerStyle.setFillForegroundColor(rbg); - headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); - // 将字体应用于样式 - headerStyle.setFont(font); - - // 在第一行创建单元格并设置样式 - Row headerRow = sheet.createRow(0); - Cell headerCell = headerRow.createCell(0); - headerCell.setCellStyle(headerStyle); - for (int i = 0; i < _map.size(); i++) { - headerCell = headerRow.createCell(i); - headerCell.setCellValue(_map.get(i).getStr("column_description")); - headerCell.setCellStyle(headerStyle); - } - - // 创建字体对象并设置字体大小为12 - font = workbook.createFont(); - font.setFontHeightInPoints((short) 12); - - // 创建单元格样式对象并将字体应用于样式 - CellStyle style = workbook.createCellStyle(); - style.setFont(font); - // 设置行高度为28 - sheet.setDefaultRowHeightInPoints(28); - // 设置每个单元格的宽度为30 - sheet.setDefaultColumnWidth(30); - // 设置边框样式 - style.setBorderTop(BorderStyle.THIN); - style.setBorderBottom(BorderStyle.THIN); - style.setBorderLeft(BorderStyle.THIN); - style.setBorderRight(BorderStyle.THIN); - // 设置水平居中 - style.setAlignment(HorizontalAlignment.CENTER); - // 设置垂直居中 - style.setVerticalAlignment(VerticalAlignment.CENTER); - - // 填充数据集的其余行 - for (int i = 1; i <= data.size(); i++) { - Row row = sheet.createRow(i); - Record r2 = data.get(i - 1); - for (int j = 0; j < _map.size(); j++) { - Cell cell = row.createCell(j); - cell.setCellValue(r2.getStr(_map.get(j).getStr("column_name"))); - cell.setCellStyle(style); - } - } - - String filePath = "c:/2.xlsx"; - // 保存Excel文件 - FileOutputStream outputStream = new FileOutputStream(filePath); - workbook.write(outputStream); - - // - System.out.println("恭喜,所有操作成功完成!"); - } else { - System.out.println("不是 Form填报任务,无法执行!"); - } } } \ No newline at end of file diff --git a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java index 2c275a51..03e475eb 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java @@ -703,19 +703,20 @@ public class CollectController extends Controller { * 0:查全市 * 1:只查市直属 * 2:查登录人员所在县区 - * + *

* bureau_type_ids * 单位类型: * 1: 下属县区教育局 * 2:下属学校 * 3:下属教辅单位 + * * @return */ @Before({GET.class}) @IsLoginInterface({}) @LayUiPageInfoInterface({"page", "limit"}) @EmptyInterface({"bureau_type_ids"}) - public void getBureauList(int query_type_id, String bureau_type_ids, String org_name, int page, int limit) { + public void getBureauList(int query_type_id, String bureau_type_ids, String org_name, int page, int limit) { //操作人员 String person_id = SessionKit.get(getRequest(), getResponse(), "person_id"); //根据人员ID,获取人员所在的单位ID @@ -728,7 +729,7 @@ public class CollectController extends Controller { //0:查全市 //1:只查市直属 //2:查登录人员所在县区 - Page list = cm.getBureauList(query_type_id, bureau_type_ids, area_id, org_name, page, limit); + Page list = cm.getBureauList(query_type_id, bureau_type_ids, area_id, org_name, page, limit); renderJson(CommonUtil.renderJsonForLayUI(list)); } @@ -1613,19 +1614,38 @@ public class CollectController extends Controller { @Before({GET.class}) @IsLoginInterface({}) @IsNumericInterface({"job_id"}) + // http://10.10.21.20:9000/QingLong/collect/getSummaryExcel?job_id=8 + // http://10.10.21.20:9000/QingLong/collect/getSummaryExcel?job_id=47 public void getSummaryExcel(int job_id) throws IOException { + //操作人员 + String person_id = SessionKit.get(getRequest(), getResponse(), "person_id"); + //根据人员ID,获取人员所在的单位ID + LoginPersonModel personModel = new LoginPersonModel(); + Record rs = personModel.getLoginInfoByPersonId(person_id); + String bureau_id = rs.get("bureau_id"); + Record jobRecord = cm.getJob(job_id); + String fabu_bureau_id = jobRecord.getStr("bureau_id"); + if (!fabu_bureau_id.equals(bureau_id)) { + Map map = new HashMap<>(); + map.put("success", false); + map.put("message", "这个任务不是登录人下发的,无法输出汇总表!"); + renderJson(map); + } String job_name = jobRecord.getStr("job_name"); - String temp_excel_filename = UUID.randomUUID().toString().toLowerCase() + ".xlsx"; - String excelPath = basePath + "/" + temp_excel_filename; + String tmpFile = UUID.randomUUID().toString().toLowerCase() + ".xlsx"; + String excelPath = basePath + "/" + tmpFile; //这里需要知道是哪种类型的,因为不同的类型,生成EXCEL汇总表的方法不同 int job_type = jobRecord.getInt("job_type");//任务类型:1:表单,2:EXCEL模板 - if (job_type == 2) { - cm.getSummaryExcel(job_id, excelPath); - //提供下载 - renderFile(new File(excelPath), "【" + job_name + "】汇总表.xlsx"); - } else { - //暂未实现 + switch (job_type) { + case 1: + cm.getSummaryExcelForFormFill(job_id, excelPath); //测试用例 job_id=8 + break; + case 2: + cm.getSummaryExcelForImportExcel(job_id, excelPath);//测试用例 job_id=47 + break; } + //提供下载 + renderFile(new File(excelPath), "【" + job_name + "】汇总表.xlsx"); } } diff --git a/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java b/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java index 1c271bc7..5b81e522 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Model/CollectModel.java @@ -903,19 +903,19 @@ public class CollectModel { if (publish_job_type_id == 1) { if (xiaShuSchool == 1) { //query_type_id=1:只查市直属 - Page list = getBureauList(1, "2", null, null, 1, 9999); + Page list = getBureauList(1, "2", null, null, 1, 9999); for (Record record : list.getList()) { bureauSet.add(record.getStr("org_id")); } } if (xiaShuJiaoFu == 1) { - Page list = getBureauList(1, "3", null, null, 1, 9999); + Page list = getBureauList(1, "3", null, null, 1, 9999); for (Record record : list.getList()) { bureauSet.add(record.getStr("org_id")); } } if (xiaShuJiaoYuJu == 1) { - Page list = getBureauList(1, "1", null, null, 1, 9999); + Page list = getBureauList(1, "1", null, null, 1, 9999); for (Record record : list.getList()) { bureauSet.add(record.getStr("org_id")); } @@ -925,13 +925,13 @@ public class CollectModel { else if (publish_job_type_id == 2) { if (xiaShuSchool == 1) { //query_type_id=2:查县区属 - Page list = getBureauList(2, "2", area_id, null, 1, 9999); + Page list = getBureauList(2, "2", area_id, null, 1, 9999); for (Record record : list.getList()) { bureauSet.add(record.getStr("org_id")); } } if (xiaShuJiaoFu == 1) { - Page list = getBureauList(2, "3", area_id, null, 1, 9999); + Page list = getBureauList(2, "3", area_id, null, 1, 9999); for (Record record : list.getList()) { bureauSet.add(record.getStr("org_id")); } @@ -1583,14 +1583,14 @@ public class CollectModel { //获取此县区的所有学校 Set targetSet = new HashSet<>(); if (xiaShuSchool == 1) { - Page list = getBureauList(2, "2", area_id, null, 1, 9999); + Page list = getBureauList(2, "2", area_id, null, 1, 9999); for (Record record : list.getList()) { targetSet.add(record.getStr("org_id")); } } //获取此县区的所有教辅单位 if (xiaShuJiaoFu == 1) { - Page list = getBureauList(2, "3", area_id, null, 1, 9999); + Page list = getBureauList(2, "3", area_id, null, 1, 9999); for (Record record : list.getList()) { targetSet.add(record.getStr("org_id")); } @@ -1731,6 +1731,17 @@ public class CollectModel { return Db.find(sql, job_id); } + /** + * 功能:获取指定列的列描述信息 + * + * @param table_name + * @return + */ + public static List getTableStructInfo(String table_name) { + String sql = "select col.column_name, col.ordinal_position as o, d.description as column_description from information_schema.columns col join pg_class c on c.relname = col.table_name left join pg_description d on d.objoid = c.oid and d.objsubid = col.ordinal_position where col.table_schema = 'public' and table_name=? order by col.ordinal_position"; + return Db.find(sql, table_name); + } + /** * 功能:生成简单的数据汇集EXCEL * @@ -1738,7 +1749,7 @@ public class CollectModel { * @param filePath * @throws IOException */ - public void getSummaryExcel(int job_id, String filePath) throws IOException { //给定任务编号,获取它有哪些表 + public void getSummaryExcelForImportExcel(int job_id, String filePath) throws IOException { //给定任务编号,获取它有哪些表 // 创建工作簿和工作表 SXSSFWorkbook workbook = new SXSSFWorkbook();//默认100行,超100行将写入临时文件 workbook.setCompressTempFiles(false); //是否压缩临时文件,否则写入速度更快,但更占磁盘,但程序最后是会将临时文件删掉的 @@ -1847,4 +1858,116 @@ public class CollectModel { FileOutputStream outputStream = new FileOutputStream(filePath); workbook.write(outputStream); } + + /** + * 功能:导出Form表单式录入的汇集数据EXCEL + * + * @param job_id + * @param filePath + * @throws IOException + */ + public void getSummaryExcelForFormFill(int job_id, String filePath) throws IOException { + Record jobRecord = getJob(job_id); + String table_name = jobRecord.getStr("form_table_name");//表格名称 + Map _map = new HashMap<>(); + List listStruct = getTableStructInfo(table_name); + int idx = 0; + for (Record r : listStruct) { + String column_name = r.getStr("column_name"); + //准备第几列用哪一个字段 + if (column_name.equals("id")) continue; + if (column_name.equals("bureau_id")) continue; + if (column_name.equals("person_id")) continue; + if (column_name.equals("class_id")) continue; + if (column_name.equals("job_id")) continue; + _map.put(idx++, r); + } + //获取数据 + List data = getTableDataByJobId(job_id, table_name); + //创建Excel + SXSSFWorkbook workbook = new SXSSFWorkbook();//默认100行,超100行将写入临时文件 + workbook.setCompressTempFiles(false); //是否压缩临时文件,否则写入速度更快,但更占磁盘,但程序最后是会将临时文件删掉的 + //开始生成EXCEL + String sheet_name = "填报情况"; + //获取数据 + Sheet sheet = workbook.createSheet(sheet_name); + // 创建单元格样式对象 + CellStyle headerStyle = workbook.createCellStyle(); + // 设置水平居中 + headerStyle.setAlignment(HorizontalAlignment.CENTER); + // 设置垂直居中 + headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); + // 设置边框样式 + headerStyle.setBorderTop(BorderStyle.THIN); + headerStyle.setBorderBottom(BorderStyle.THIN); + headerStyle.setBorderLeft(BorderStyle.THIN); + headerStyle.setBorderRight(BorderStyle.THIN); + + // 创建字体对象并设置字体颜色为白色 + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 14); + font.setBold(true); + font.setFontName("黑体"); + font.setColor(IndexedColors.BLACK.getIndex()); + // 设置背景色为浅蓝色 #d2f4f2 + //颜色 + String str = "#d2f4f2"; + String sr = str.substring(1, 3); + String sg = str.substring(3, 5); + String sb = str.substring(5, 7); + //16进制的字符串转为int + int r = Integer.parseInt(sr, 16); + int g = Integer.parseInt(sg, 16); + int b = Integer.parseInt(sb, 16); + XSSFColor rbg = new XSSFColor(new java.awt.Color(r, g, b), new DefaultIndexedColorMap()); + headerStyle.setFillForegroundColor(rbg); + headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + // 将字体应用于样式 + headerStyle.setFont(font); + + // 在第一行创建单元格并设置样式 + Row headerRow = sheet.createRow(0); + Cell headerCell = headerRow.createCell(0); + headerCell.setCellStyle(headerStyle); + for (int i = 0; i < _map.size(); i++) { + headerCell = headerRow.createCell(i); + headerCell.setCellValue(_map.get(i).getStr("column_description")); + headerCell.setCellStyle(headerStyle); + } + + // 创建字体对象并设置字体大小为12 + font = workbook.createFont(); + font.setFontHeightInPoints((short) 12); + + // 创建单元格样式对象并将字体应用于样式 + CellStyle style = workbook.createCellStyle(); + style.setFont(font); + // 设置行高度为28 + sheet.setDefaultRowHeightInPoints(28); + // 设置每个单元格的宽度为30 + sheet.setDefaultColumnWidth(30); + // 设置边框样式 + style.setBorderTop(BorderStyle.THIN); + style.setBorderBottom(BorderStyle.THIN); + style.setBorderLeft(BorderStyle.THIN); + style.setBorderRight(BorderStyle.THIN); + // 设置水平居中 + style.setAlignment(HorizontalAlignment.CENTER); + // 设置垂直居中 + style.setVerticalAlignment(VerticalAlignment.CENTER); + + // 填充数据集的其余行 + for (int i = 1; i <= data.size(); i++) { + Row row = sheet.createRow(i); + Record r2 = data.get(i - 1); + for (int j = 0; j < _map.size(); j++) { + Cell cell = row.createCell(j); + cell.setCellValue(r2.getStr(_map.get(j).getStr("column_name"))); + cell.setCellStyle(style); + } + } + // 保存Excel文件 + FileOutputStream outputStream = new FileOutputStream(filePath); + workbook.write(outputStream); + } } \ No newline at end of file