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