From eb32dc24c5e9f6768d33b682c07842b545a9b942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=B5=B7?= <10402852@qq.com> Date: Thu, 11 Jan 2024 21:59:48 +0800 Subject: [PATCH] 'commit' --- pom.xml | 2 +- src/main/java/UnitTest/TestExportExcel.java | 130 ++++++++++++++++++ .../Collect/Controller/CollectController.java | 2 +- .../com/dsideal/QingLong/Util/PoiUtil.java | 3 +- 4 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 src/main/java/UnitTest/TestExportExcel.java diff --git a/pom.xml b/pom.xml index 3f379521..aa99223d 100644 --- a/pom.xml +++ b/pom.xml @@ -243,7 +243,7 @@ commons-io commons-io - 2.11.0 + 2.14.0 org.apache.commons diff --git a/src/main/java/UnitTest/TestExportExcel.java b/src/main/java/UnitTest/TestExportExcel.java new file mode 100644 index 00000000..384ebe15 --- /dev/null +++ b/src/main/java/UnitTest/TestExportExcel.java @@ -0,0 +1,130 @@ +package UnitTest; + +import com.jfinal.plugin.activerecord.ActiveRecordPlugin; +import com.jfinal.plugin.activerecord.Db; +import com.jfinal.plugin.activerecord.Record; +import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect; +import com.jfinal.plugin.hikaricp.HikariCpPlugin; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class TestExportExcel { + public static void main(String[] args) { + //读取库 + HikariCpPlugin hp = new HikariCpPlugin("jdbc:postgresql://10.10.14.71:5432/szjz_db", "postgres", + "DsideaL147258369", "org.postgresql.Driver"); + hp.start(); + + ActiveRecordPlugin arp = new ActiveRecordPlugin(hp); + arp.setDialect(new PostgreSqlDialect()); + arp.start(); + //表名 + String table_name = "ds_job_5_0"; + //读取表的结构信息 + String sql = "select column_name,excel_column_idx,original_name from t_collect_mapping where table_name= ?"; + List list = Db.find(sql, table_name); + Map _map = new HashMap<>(); + for (Record record : list) { + Record r = new Record(); + r.set("column_name", record.getStr("column_name")); + r.set("original_name", record.getStr("original_name")); + _map.put(record.getInt("excel_column_idx") + 1, r); + } + //读取表的注释 + sql = "SELECT obj_description(?::regclass, 'pg_class') as memo"; + String memo = Db.findFirst(sql, table_name).getStr("memo"); + + //获取数据 + sql = "select t1.*,t2.org_name as bureau_name from " + table_name + " as t1 inner join t_base_organization as t2 on t1.bureau_id=t2.bureau_id"; + List data = Db.find(sql); + + + // 创建工作簿和工作表 + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet(memo); + + // 创建单元格样式对象 + 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 + headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + // 将字体应用于样式 + headerStyle.setFont(font); + + // 在第一行创建单元格并设置样式 + Row headerRow = sheet.createRow(0); + Cell headerCell = headerRow.createCell(0); + headerCell.setCellValue("单位"); + headerCell.setCellStyle(headerStyle); + for (int i = 0; i < list.size(); i++) { + headerCell = headerRow.createCell(i + 1); + headerCell.setCellValue(_map.get(i + 1).getStr("original_name")); + 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 record = data.get(i - 1); + Cell cell = row.createCell(0); + cell.setCellValue(record.getStr("bureau_name")); + cell.setCellStyle(style); + for (int j = 0; j < list.size(); j++) { + cell = row.createCell(j + 1); + cell.setCellValue(record.getStr(_map.get(j + 1).getStr("column_name"))); + cell.setCellStyle(style); + } + } + + // 保存Excel文件 + try (FileOutputStream outputStream = new FileOutputStream("c:/example.xlsx")) { + workbook.write(outputStream); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ 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 47422838..0f2b86ef 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java @@ -552,7 +552,7 @@ public class CollectController extends Controller { Record r = cm.getSheetConfig(upload_excel_filename_finish, i); int data_start_row = r.getInt("data_start_row"); //恢复背景色 - PoiUtil.resetStyle(wb, sheet, data_start_row); + PoiUtil.resetStyle(sheet, data_start_row); //数据有效行数 int lastRowNum = sheet.getLastRowNum(); diff --git a/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java b/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java index 64070f86..329b81b0 100644 --- a/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java +++ b/src/main/java/com/dsideal/QingLong/Util/PoiUtil.java @@ -86,10 +86,9 @@ public class PoiUtil { /** * 功能:将指定Sheet表中所有数据区域进行样式还原 * - * @param wb * @param sheet */ - public static void resetStyle(XSSFWorkbook wb, XSSFSheet sheet, int dataStartRow) { + public static void resetStyle(XSSFSheet sheet, int dataStartRow) { // 遍历所有行和单元格,恢复颜色 for (int row = dataStartRow; row <= sheet.getLastRowNum(); row++) { for (int col = 0; col < sheet.getRow(row).getLastCellNum(); col++) {