parent
f27e92551e
commit
2750c972e6
Binary file not shown.
Binary file not shown.
@ -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<Integer> 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();
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
Loading…
Reference in new issue