参考链接:
导出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 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
顺便贴上我的github地址:https://github.com/gitUserMandjq/tool
在JdbcTest类里执行方法,生成效果如下

猜你喜欢
网友评论
- 搜索
- 最新文章
- 热门文章
