导出Mysql数据库表名和字段并合并成一个word

参考链接:

导出MySQL数据库所有库和字段注释及相关信息为word文档------工具类

java - Apache POI - How to copy tables from one docx to another docx - Stack Overflow

领导让我研究下一个低代码平台的代码,我就想着做一个把数据库字段直接导出来的工具。百度了一份代码是分表导出的,每个表都一个单独的word,用着不方便,我就在这个基础上加了个合并word的功能。说起来还是外网的质量高,一搜就搜到了,在百度上找了半天不行。

    public static void main(String[] args) throws Exception {
        // 创建新的空白Word文档作为目标文件
        XWPFDocument mergedDoc = new XWPFDocument();

        // 定义需要合并的源文件路径列表
        String prefix = "D:\\要合并的文件夹\\";
        File fold = new File(prefix);
        File[] fs = fold.listFiles();
        for(File file:fs){
            FileInputStream fis = new FileInputStream(file);
            // 读取每个源文件中的内容
            XWPFDocument doc = new XWPFDocument(fis);
            copyElements(doc, mergedDoc);
            // 关闭输入流
            fis.close();
        }

        // 保存合并后的文档
        FileOutputStream fos = new FileOutputStream(prefix + "\\000merged.docx");
        mergedDoc.write(fos);
        fos.close();

        System.out.println("合并完成!");
    }
    public static void copyElements(XWPFDocument sourceDocument, XWPFDocument targetDocument) {
        List<IBodyElement> elements = sourceDocument.getBodyElements();
        for (IBodyElement element : elements) {
            if (element instanceof XWPFParagraph) {
                XWPFParagraph paragraph = (XWPFParagraph) element;
                copyParagraph(paragraph, targetDocument.createParagraph());
            } else if (element instanceof XWPFTable) {
                XWPFTable table = (XWPFTable) element;
                copyTable(table, targetDocument.createTable());
            }
        }
    }
    private static void copyTable(XWPFTable source, XWPFTable target) {
        target.getCTTbl().setTblPr(source.getCTTbl().getTblPr());
        target.getCTTbl().setTblGrid(source.getCTTbl().getTblGrid());
        for (int r = 0; r<source.getRows().size(); r++) {
            XWPFTableRow targetRow = target.createRow();
            XWPFTableRow row = source.getRows().get(r);
            targetRow.getCtRow().setTrPr(row.getCtRow().getTrPr());
            for (int c=0; c<row.getTableCells().size(); c++) {
                //newly created row has 1 cell
                XWPFTableCell targetCell = c==0 ? targetRow.getTableCells().get(0) : targetRow.createCell();
                XWPFTableCell cell = row.getTableCells().get(c);
                targetCell.getCTTc().setTcPr(cell.getCTTc().getTcPr());
                XmlCursor cursor = targetCell.getParagraphArray(0).getCTP().newCursor();
                for (int p = 0; p < cell.getBodyElements().size(); p++) {
                    IBodyElement elem = cell.getBodyElements().get(p);
                    if (elem instanceof XWPFParagraph) {
                        XWPFParagraph targetPar = targetCell.insertNewParagraph(cursor);
                        cursor.toNextToken();
                        XWPFParagraph par = (XWPFParagraph) elem;
                        copyParagraph(par, targetPar);
                    } else if (elem instanceof XWPFTable) {
                        XWPFTable targetTable = targetCell.insertNewTbl(cursor);
                        XWPFTable table = (XWPFTable) elem;
                        copyTable(table, targetTable);
                        cursor.toNextToken();
                    }
                }
                //newly created cell has one default paragraph we need to remove
                targetCell.removeParagraph(targetCell.getParagraphs().size()-1);
            }
        }
        //newly created table has one row by default. we need to remove the default row.
        target.removeRow(0);
    }
    private static void copyParagraph(XWPFParagraph source, XWPFParagraph target) {
        target.getCTP().setPPr(source.getCTP().getPPr());
        for (int i=0; i<source.getRuns().size(); i++ ) {
            XWPFRun run = source.getRuns().get(i);
            XWPFRun targetRun = target.createRun();
            //copy formatting
            targetRun.getCTR().setRPr(run.getCTR().getRPr());
            //no images just copy text
            targetRun.setText(run.getText(0));
        }
    }

顺便贴上我的github地址:https://github.com/gitUserMandjq/tool

在JdbcTest类里执行方法,生成效果如下

相关推荐
远歌已逝2 小时前
维护在线重做日志(二)
数据库·oracle
qq_433099403 小时前
Ubuntu20.04从零安装IsaacSim/IsaacLab
数据库
Dlwyz3 小时前
redis-击穿、穿透、雪崩
数据库·redis·缓存
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
工业甲酰苯胺5 小时前
Redis性能优化的18招
数据库·redis·性能优化
冰帝海岸5 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象5 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic6 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端