diff --git a/WebRoot/Excel/397b04f7-f0cb-41ba-9638-23b9817b00c3.xlsx b/WebRoot/Excel/397b04f7-f0cb-41ba-9638-23b9817b00c3.xlsx new file mode 100644 index 00000000..bb7ac7ef Binary files /dev/null and b/WebRoot/Excel/397b04f7-f0cb-41ba-9638-23b9817b00c3.xlsx differ diff --git a/WebRoot/Excel/81207c6b-2eb3-4c93-ae52-cd5ba91e1558.xlsx b/WebRoot/Excel/81207c6b-2eb3-4c93-ae52-cd5ba91e1558.xlsx new file mode 100644 index 00000000..18212ebf Binary files /dev/null and b/WebRoot/Excel/81207c6b-2eb3-4c93-ae52-cd5ba91e1558.xlsx differ diff --git a/src/main/java/UnitTest/ImportExcel/Test/ExportExcelStruct.java b/src/main/java/UnitTest/ImportExcel/Test/ExportExcelStruct.java new file mode 100644 index 00000000..9ad8b084 --- /dev/null +++ b/src/main/java/UnitTest/ImportExcel/Test/ExportExcelStruct.java @@ -0,0 +1,184 @@ +package UnitTest.ImportExcel.Test; + +import com.dsideal.QingLong.Start; +import com.jfinal.kit.PropKit; +import com.jfinal.kit.StrKit; +import com.jfinal.plugin.activerecord.ActiveRecordPlugin; +import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory; +import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect; +import com.jfinal.plugin.hikaricp.HikariCpPlugin; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.DefaultIndexedColorMap; +import org.apache.poi.xssf.usermodel.IndexedColorMap; +import org.apache.poi.xssf.usermodel.XSSFColor; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.sql.*; +import java.util.HashSet; +import java.util.Set; + +public class ExportExcelStruct { + public static void main(String[] args) throws IOException { + //告之配置文件位置 + PropKit.use("application.properties"); + HikariCpPlugin hp = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), + PropKit.get("password").trim(), PropKit.get("driverClassName")); + hp.start(); + // 配置ActiveRecord插件 + ActiveRecordPlugin arp = new ActiveRecordPlugin(hp); + //配置默认小写 + arp.setContainerFactory(new CaseInsensitiveContainerFactory(true)); + + arp.setDialect(new PostgreSqlDialect()); + //遍历sql目录下所有的sql文件 + File sqlDir; + String basePath = Start.class.getClassLoader().getResource(".").getPath(); + sqlDir = new File(basePath + "/Sql"); + File[] sqlFiles = sqlDir.listFiles(); + for (File sqlFile : sqlFiles != null ? sqlFiles : new File[0]) { + //只加载.sql文件 + if (sqlFile.getName().indexOf(".sql") > 0) { + arp.addSqlTemplate("/Sql/" + sqlFile.getName()); + } + } + arp.start(); + + String url = PropKit.get("jdbcUrl"); + String user = PropKit.get("user"); + String password = PropKit.get("password"); + + try (Connection conn = DriverManager.getConnection(url, user, password)) { + DatabaseMetaData metaData = conn.getMetaData(); + String[] types = {"TABLE"}; + ResultSet tables = metaData.getTables(null, null, "ds_%", types); + + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("Table Information"); + int rowNum = 0; + + Set isHead = new HashSet<>(); + while (tables.next()) { + String tableName = tables.getString("TABLE_NAME"); + String tableComment = tables.getString("REMARKS"); // 从表的comment中读取注释信息 + ResultSet columns = metaData.getColumns(null, null, tableName, null); + + // 创建表名称的合并单元格 + sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 5)); + Row headerRow = sheet.createRow(rowNum++); + if (!StrKit.isBlank(tableComment)) { + headerRow.createCell(0).setCellValue("表名: " + tableName + " 【" + tableComment + "】"); + } else { + headerRow.createCell(0).setCellValue("表名: " + tableName); + } + CellStyle headerStyle = workbook.createCellStyle(); + + //设置自定义颜色 + headerStyle.setAlignment(HorizontalAlignment.CENTER); + headerStyle.setBorderTop(BorderStyle.THIN); + headerStyle.setBorderBottom(BorderStyle.THIN); + headerStyle.setBorderLeft(BorderStyle.THIN); + headerStyle.setBorderRight(BorderStyle.THIN); + headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); + Font headerFont = workbook.createFont(); + headerFont.setBold(true); + headerFont.setFontName("黑体"); + headerFont.setFontHeightInPoints((short) 18); + headerStyle.setFont(headerFont); + headerRow.getCell(0).setCellStyle(headerStyle); + headerRow.setHeightInPoints(38); // 设置行高 + isHead.add(headerRow.getRowNum()); + + // 创建表格标题 + Row titleRow = sheet.createRow(rowNum++); + titleRow.createCell(0).setCellValue("序号"); + titleRow.createCell(1).setCellValue("字段名"); + titleRow.createCell(2).setCellValue("数据类型"); + titleRow.createCell(3).setCellValue("字段长度"); + titleRow.createCell(4).setCellValue("是否允许空"); + titleRow.createCell(5).setCellValue("描述"); + + // 设置标题样式 + CellStyle titleStyle = workbook.createCellStyle(); + //颜色 + 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()); + titleStyle.setFillForegroundColor(rbg); + titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleStyle.setAlignment(HorizontalAlignment.CENTER); + titleStyle.setBorderTop(BorderStyle.THIN); + titleStyle.setBorderBottom(BorderStyle.THIN); + titleStyle.setBorderLeft(BorderStyle.THIN); + titleStyle.setBorderRight(BorderStyle.THIN); + titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); + Font titleFont = workbook.createFont(); + titleFont.setFontName("宋体"); + titleFont.setBold(true); + titleFont.setFontHeightInPoints((short) 15); + titleStyle.setFont(titleFont); + for (int i = 0; i < 6; i++) { + titleRow.getCell(i).setCellStyle(titleStyle); + } + + // 设置列宽 + sheet.setColumnWidth(0, 10 * 256); // 序号列宽度 + sheet.setColumnWidth(1, 40 * 256); // 20个字符宽度 + sheet.setColumnWidth(2, 20 * 256); + sheet.setColumnWidth(3, 15 * 256); + sheet.setColumnWidth(4, 20 * 256); + sheet.setColumnWidth(5, 40 * 256); + + // 创建表格数据 + int serialNumber = 1; + while (columns.next()) { + Row dataRow = sheet.createRow(rowNum++); + dataRow.createCell(0).setCellValue(serialNumber++); + dataRow.createCell(1).setCellValue(columns.getString("COLUMN_NAME")); + dataRow.createCell(2).setCellValue(columns.getString("TYPE_NAME")); + dataRow.createCell(3).setCellValue(columns.getString("COLUMN_SIZE")); // 添加字段长度 + dataRow.createCell(4).setCellValue(columns.getString("IS_NULLABLE")); + dataRow.createCell(5).setCellValue(columns.getString("REMARKS")); + + for (int i = 0; i < 6; i++) { + Cell cell = dataRow.getCell(i); + CellStyle dataStyle = workbook.createCellStyle(); + dataStyle.setAlignment(HorizontalAlignment.CENTER); + dataStyle.setBorderTop(BorderStyle.THIN); + dataStyle.setBorderBottom(BorderStyle.THIN); + dataStyle.setBorderLeft(BorderStyle.THIN); + dataStyle.setBorderRight(BorderStyle.THIN); + dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); + + Font dataFont = workbook.createFont(); + dataFont.setFontName("宋体"); + dataFont.setFontHeightInPoints((short) 13); + dataStyle.setFont(dataFont); + cell.setCellStyle(dataStyle); + } + } + } + + // 设置其它行的高度 + for (int i = 0; i < rowNum; i++) { + if (!isHead.contains(i)) sheet.getRow(i).setHeightInPoints(28); // 设置行高 + } + + FileOutputStream fileOut = new FileOutputStream("c://table_information.xlsx"); + workbook.write(fileOut); + fileOut.close(); + workbook.close(); + } catch (SQLException | IOException e) { + e.printStackTrace(); + } + } +} 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 5fd927bb..ac4f9895 100644 --- a/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java +++ b/src/main/java/com/dsideal/QingLong/Collect/Controller/CollectController.java @@ -19,22 +19,24 @@ import com.jfinal.core.Controller; import com.jfinal.ext.interceptor.GET; import com.jfinal.ext.interceptor.POST; import com.jfinal.kit.Kv; +import com.jfinal.kit.PropKit; import com.jfinal.kit.StrKit; +import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Page; import com.jfinal.plugin.activerecord.Record; import com.jfinal.upload.UploadFile; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentInformation; import org.apache.poi.openxml4j.util.ZipSecureFile; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.xssf.usermodel.XSSFCell; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.usermodel.*; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; +import java.sql.*; import java.text.ParseException; import java.util.*; @@ -1094,4 +1096,164 @@ public class CollectController extends Controller { List list = cm.getGroup(group_id); renderJson(CommonUtil.renderJsonForLayUI(list)); } -} \ No newline at end of file + + /** + * 功能:导出指定任务的表结构EXCEL + * + * @param job_id http://10.10.21.20:9000/QingLong/collect/exportTableStructExcel?job_id=1 + */ + @Before({GET.class}) + @IsNumericInterface({"job_id"}) + public void exportTableStructExcel(int job_id) { + String job_name = cm.getJob(job_id).getStr("job_name"); + String excelPath = basePath + File.separator + UUID.randomUUID().toString().toLowerCase() + ".xlsx"; + String url = PropKit.get("jdbcUrl"); + String user = PropKit.get("user"); + String password = PropKit.get("password"); + + List jobTables = new ArrayList<>(); + + //当前 job_id下有哪些表 + String sql = "select form_table_name from t_collect_job where job_id=?"; + String form_table_name = Db.findFirst(sql, job_id).getStr("form_table_name"); + if (!StrKit.isBlank(form_table_name)) jobTables.add(form_table_name); + //sheet型 + sql = "select table_name from t_collect_job_sheet where job_id=?"; + List list = Db.find(sql, job_id); + for (Record record : list) { + jobTables.add(record.getStr("table_name")); + } + + try (Connection conn = DriverManager.getConnection(url, user, password)) { + DatabaseMetaData metaData = conn.getMetaData(); + String[] types = {"TABLE"}; + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet(job_name); + ResultSet tables = metaData.getTables(null, null, "ds_%", types); + + int rowNum = 0; + Set isHead = new HashSet<>(); + while (tables.next()) { + String tableName = tables.getString("TABLE_NAME"); + if (!jobTables.contains(tableName)) continue; + + String tableComment = tables.getString("REMARKS"); // 从表的comment中读取注释信息 + ResultSet columns = metaData.getColumns(null, null, tableName, null); + + // 创建表名称的合并单元格 + sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 5)); + Row headerRow = sheet.createRow(rowNum++); + if (!StrKit.isBlank(tableComment)) { + headerRow.createCell(0).setCellValue("表名: " + tableName + " 【" + tableComment + "】"); + } else { + headerRow.createCell(0).setCellValue("表名: " + tableName); + } + CellStyle headerStyle = workbook.createCellStyle(); + + //设置自定义颜色 + headerStyle.setAlignment(HorizontalAlignment.CENTER); + headerStyle.setBorderTop(BorderStyle.THIN); + headerStyle.setBorderBottom(BorderStyle.THIN); + headerStyle.setBorderLeft(BorderStyle.THIN); + headerStyle.setBorderRight(BorderStyle.THIN); + headerStyle.setVerticalAlignment(VerticalAlignment.CENTER); + Font headerFont = workbook.createFont(); + headerFont.setBold(true); + headerFont.setFontName("黑体"); + headerFont.setFontHeightInPoints((short) 18); + headerStyle.setFont(headerFont); + headerRow.getCell(0).setCellStyle(headerStyle); + headerRow.setHeightInPoints(38); // 设置行高 + isHead.add(headerRow.getRowNum()); + + // 创建表格标题 + Row titleRow = sheet.createRow(rowNum++); + titleRow.createCell(0).setCellValue("序号"); + titleRow.createCell(1).setCellValue("字段名"); + titleRow.createCell(2).setCellValue("数据类型"); + titleRow.createCell(3).setCellValue("字段长度"); + titleRow.createCell(4).setCellValue("是否允许空"); + titleRow.createCell(5).setCellValue("描述"); + + // 设置标题样式 + CellStyle titleStyle = workbook.createCellStyle(); + //颜色 + 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()); + titleStyle.setFillForegroundColor(rbg); + titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleStyle.setAlignment(HorizontalAlignment.CENTER); + titleStyle.setBorderTop(BorderStyle.THIN); + titleStyle.setBorderBottom(BorderStyle.THIN); + titleStyle.setBorderLeft(BorderStyle.THIN); + titleStyle.setBorderRight(BorderStyle.THIN); + titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); + Font titleFont = workbook.createFont(); + titleFont.setFontName("宋体"); + titleFont.setBold(true); + titleFont.setFontHeightInPoints((short) 15); + titleStyle.setFont(titleFont); + for (int j = 0; j < 6; j++) { + titleRow.getCell(j).setCellStyle(titleStyle); + } + + // 设置列宽 + sheet.setColumnWidth(0, 10 * 256); // 序号列宽度 + sheet.setColumnWidth(1, 40 * 256); // 20个字符宽度 + sheet.setColumnWidth(2, 20 * 256); + sheet.setColumnWidth(3, 15 * 256); + sheet.setColumnWidth(4, 20 * 256); + sheet.setColumnWidth(5, 40 * 256); + + // 创建表格数据 + int serialNumber = 1; + while (columns.next()) { + Row dataRow = sheet.createRow(rowNum++); + dataRow.createCell(0).setCellValue(serialNumber++); + dataRow.createCell(1).setCellValue(columns.getString("COLUMN_NAME")); + dataRow.createCell(2).setCellValue(columns.getString("TYPE_NAME")); + dataRow.createCell(3).setCellValue(columns.getString("COLUMN_SIZE")); // 添加字段长度 + dataRow.createCell(4).setCellValue(columns.getString("IS_NULLABLE")); + dataRow.createCell(5).setCellValue(columns.getString("REMARKS")); + + for (int j = 0; j < 6; j++) { + Cell cell = dataRow.getCell(j); + CellStyle dataStyle = workbook.createCellStyle(); + dataStyle.setAlignment(HorizontalAlignment.CENTER); + dataStyle.setBorderTop(BorderStyle.THIN); + dataStyle.setBorderBottom(BorderStyle.THIN); + dataStyle.setBorderLeft(BorderStyle.THIN); + dataStyle.setBorderRight(BorderStyle.THIN); + dataStyle.setVerticalAlignment(VerticalAlignment.CENTER); + + Font dataFont = workbook.createFont(); + dataFont.setFontName("宋体"); + dataFont.setFontHeightInPoints((short) 13); + dataStyle.setFont(dataFont); + cell.setCellStyle(dataStyle); + } + } + } + // 设置其它行的高度 + for (int i = 0; i < rowNum; i++) { + if (!isHead.contains(i)) sheet.getRow(i).setHeightInPoints(28); // 设置行高 + } + + FileOutputStream fileOut = new FileOutputStream(excelPath); + workbook.write(fileOut); + fileOut.close(); + workbook.close(); + //提供下载 + renderFile(new File(excelPath), job_name + "表结构.xlsx"); + } catch (SQLException | IOException e) { + e.printStackTrace(); + } + } +} diff --git a/table_information.xlsx b/table_information.xlsx new file mode 100644 index 00000000..397d5005 Binary files /dev/null and b/table_information.xlsx differ