导出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类里执行方法,生成效果如下

相关推荐
Acrelhuang16 分钟前
安科瑞5G基站直流叠光监控系统-安科瑞黄安南
大数据·数据库·数据仓库·物联网
雷神乐乐16 分钟前
File.separator与File.separatorChar的区别
java·路径分隔符
小刘|20 分钟前
《Java 实现希尔排序:原理剖析与代码详解》
java·算法·排序算法
逊嘘39 分钟前
【Java语言】抽象类与接口
java·开发语言·jvm
morris1311 小时前
【SpringBoot】Xss的常见攻击方式与防御手段
java·spring boot·xss·csp
十叶知秋1 小时前
【jmeter】jmeter的线程组功能的详细介绍
数据库·jmeter·性能测试
七星静香1 小时前
laravel chunkById 分块查询 使用时的问题
java·前端·laravel
Jacob程序员1 小时前
java导出word文件(手绘)
java·开发语言·word
ZHOUPUYU1 小时前
IntelliJ IDEA超详细下载安装教程(附安装包)
java·ide·intellij-idea
q2498596931 小时前
前端预览word、excel、ppt
前端·word·excel