|
|
@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
package com.dsideal.Res.Test;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MetadataTemplateGenerator {
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
// 创建工作簿
|
|
|
|
|
|
|
|
Workbook workbook = new XSSFWorkbook();
|
|
|
|
|
|
|
|
Sheet sheet = workbook.createSheet("元数据模板");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建标题行样式
|
|
|
|
|
|
|
|
CellStyle headerStyle = workbook.createCellStyle();
|
|
|
|
|
|
|
|
Font headerFont = workbook.createFont();
|
|
|
|
|
|
|
|
headerFont.setBold(true);
|
|
|
|
|
|
|
|
headerStyle.setFont(headerFont);
|
|
|
|
|
|
|
|
headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
|
|
|
|
|
headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
|
|
|
headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
|
|
|
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
|
|
|
headerStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
headerStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
|
|
|
headerStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
|
|
|
headerStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建数据单元格样式
|
|
|
|
|
|
|
|
CellStyle dataStyle = workbook.createCellStyle();
|
|
|
|
|
|
|
|
dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
|
|
|
dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
|
|
|
dataStyle.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
dataStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
|
|
|
dataStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
|
|
|
dataStyle.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建标题行
|
|
|
|
|
|
|
|
String[] headers = {"系统名称", "表名", "字段名", "数据类型", "长度", "是否为空", "是否主键", "默认值", "描述", "创建时间", "更新时间", "负责人"};
|
|
|
|
|
|
|
|
Row headerRow = sheet.createRow(0);
|
|
|
|
|
|
|
|
headerRow.setHeightInPoints(20);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 先设置所有列宽为44(原22的两倍)
|
|
|
|
|
|
|
|
for (int i = 0; i < headers.length; i++) {
|
|
|
|
|
|
|
|
sheet.setColumnWidth(i, 44 * 256); // 44个字符宽度
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 再创建单元格
|
|
|
|
|
|
|
|
for (int i = 0; i < headers.length; i++) {
|
|
|
|
|
|
|
|
Cell cell = headerRow.createCell(i);
|
|
|
|
|
|
|
|
cell.setCellValue(headers[i]);
|
|
|
|
|
|
|
|
cell.setCellStyle(headerStyle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置数据行样式
|
|
|
|
|
|
|
|
for (int i = 1; i <= 1000; i++) {
|
|
|
|
|
|
|
|
Row row = sheet.createRow(i);
|
|
|
|
|
|
|
|
row.setHeightInPoints(20); // 设置行高
|
|
|
|
|
|
|
|
for (int j = 0; j < headers.length; j++) {
|
|
|
|
|
|
|
|
Cell cell = row.createCell(j);
|
|
|
|
|
|
|
|
cell.setCellStyle(dataStyle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置数据验证(是否为空、是否主键)
|
|
|
|
|
|
|
|
DataValidationHelper validationHelper = sheet.getDataValidationHelper();
|
|
|
|
|
|
|
|
DataValidationConstraint constraint = validationHelper.createExplicitListConstraint(new String[]{"是", "否"});
|
|
|
|
|
|
|
|
CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, 5, 6);
|
|
|
|
|
|
|
|
DataValidation validation = validationHelper.createValidation(constraint, addressList);
|
|
|
|
|
|
|
|
sheet.addValidationData(validation);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 自动调整列宽
|
|
|
|
|
|
|
|
for (int i = 0; i < headers.length; i++) {
|
|
|
|
|
|
|
|
sheet.autoSizeColumn(i);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 保存文件
|
|
|
|
|
|
|
|
try (FileOutputStream outputStream = new FileOutputStream("c:/元数据采集模板.xlsx")) {
|
|
|
|
|
|
|
|
workbook.write(outputStream);
|
|
|
|
|
|
|
|
System.out.println("Excel模板生成成功!");
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|