MySQL表结构导出到Word

Apache POI是基于Office Open XML标准(OOXML)和Microsoft的OLE2复合文档格式(OLE2)处理各种文件格式的开源项目。支持Word、Excel、PowerPoint格式文档的读写,Visio、Publisher、Outlook格式文档的读取。

使用POI导出数据库表步骤:
1、先获取目标数据库所有表和所有字段,存储到List。
如数据库:jsbb

SELECT
    isc.table_name,
    isc.column_name,
    isc.column_type,
    isc.is_nullable,
    isc.column_key,
    isc.column_comment,
    ist.table_comment,
    ist.create_time 
FROM
    information_schema.COLUMNS isc
    JOIN information_schema.TABLES ist ON isc.table_name = ist.table_name 
    AND ist.table_schema = 'jsbb' 
WHERE
    isc.table_schema = 'jsbb';

2、迭代数据输出到Word
将数据按表名分组拆分到Map。Map的key为表名,value为该表的字段集合。

//主要代码
Map<String, List<Map<String, String>>> map = list.stream().collect(Collectors.groupingBy(e -> e.get("tableName"), TreeMap::new, Collectors.toList()));
OutputStream os = new FileOutputStream(new File("数据库表设计.docx"));
XWPFDocument document = new XWPFDocument();
int i = 0;
Iterator tableIt = map.entrySet().iterator();
while (tableIt.hasNext()) {
    Map.Entry entry = (Map.Entry) tableIt.next();
    List<Map<String, String>> columnList = (List<Map<String, String>>) entry.getValue();
    String tableName = columnList.get(0).get("tableName");
    if (!tableNameList.contains(tableName)) {
        continue;
    }
    ++i;
    XWPFRun run = createRun(document, "1");
    run.setBold(true);
    run.setFontSize(15);
    run.setText(i + "." + tableName + ":" + columnList.get(0).get("tableComment"));
    XWPFTable requestTable = document.createTable(1, 5);
    requestTable.getCTTbl().addNewTblPr().addNewTblW().setW(BigInteger.valueOf(8000));
    requestTable.getRow(0).getTableCells().stream().forEach(o -> {
        o.setColor("cccccc");
        o.setWidth("20%");
    });
    createRun(requestTable, 0, 0, ParagraphAlignment.CENTER, "字段名称");
    createRun(requestTable, 0, 1, ParagraphAlignment.CENTER, "描述");
    createRun(requestTable, 0, 2, ParagraphAlignment.CENTER, "类型");
    createRun(requestTable, 0, 3, ParagraphAlignment.CENTER, "是否可为空");
    createRun(requestTable, 0, 4, ParagraphAlignment.CENTER, "是否主键");
    for (int k = 0; k < columnList.size(); k++) {
        Map<String, String> parameter = columnList.get(k);
        requestTable.createRow();
        createRun(requestTable, k + 1, 0, ParagraphAlignment.CENTER, parameter.get("columnName"));
        createRun(requestTable, k + 1, 1, ParagraphAlignment.CENTER, parameter.get("columnComment"));
        createRun(requestTable, k + 1, 2, ParagraphAlignment.CENTER, parameter.get("columnType"));
        createRun(requestTable, k + 1, 3, ParagraphAlignment.CENTER, "NO".equals(parameter.get("isNullable")) ? "否" : "是");
        createRun(requestTable, k + 1, 4, ParagraphAlignment.CENTER, "PRI".equals(parameter.get("columnKey")) ? "是" : "");
    }
}
document.write(os);
os.close();

private static void createRun(XWPFTable requestTable, int row, int cell, ParagraphAlignment align, String value) {
    XWPFParagraph paragraph = requestTable.getRow(row).getCell(cell).getParagraphs().get(0);
    paragraph.setAlignment(align);
    paragraph.createRun().setText(value);
}

效果图
image

代码:https://gitee.com/jsbb/jsbb

posted on 2018-07-20 14:00  lc19149  阅读(633)  评论(0编辑  收藏  举报

导航