poi - 拷贝部分内容到其他word文件

1 篇文章 0 订阅
1 篇文章 0 订阅
注意:这是简单实现,主要是对table,段落的实现,图片的拷贝,sdt的拷贝会在之后的文档中实现


<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.12.1</version>
</dependency>
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class WriteOutWord {
    
    private static String def_path = "自己路劲"+System.currentTimeMillis()+".docx";

    public static void writeElementOut(List<IBodyElement> bodyElements,String path) {

        XWPFDocument newDoc = generateNewXWPFDoc(bodyElements);
        if(StringUtils.isBlank(path)){
            path = def_path;
        }
        writeOutFile(newDoc,path);
    }
/**
 * 生成新的XWPFDocument对象
 * @param bodyElements
 * @return
 */
private static XWPFDocument generateNewXWPFDoc(List<IBodyElement> bodyElements) {

    XWPFDocument newDoc = new XWPFDocument();
    for (int i = 0; i < bodyElements.size(); i++) {
        IBodyElement bodyElement = bodyElements.get(i);
        elementFitAndSet(newDoc,bodyElement);
    }
    return newDoc;
}

/**
 * 匹配文本类型
 * @param newDoc
 * @param bodyElement
 */
private static void elementFitAndSet(XWPFDocument newDoc,IBodyElement bodyElement){
    if(bodyElement instanceof XWPFParagraph){
        copyParagraph(newDoc,bodyElement);
    }else if(bodyElement instanceof XWPFTable){
        setTable(newDoc,bodyElement);
    }else {
        throw new RuntimeException("其余文本类型,还需要扩展");
    }
}

/**
 * copy 段落组件
 * @param newDoc
 * @param bodyElement
 */
private static void copyParagraph(XWPFDocument newDoc,IBodyElement bodyElement){
    XWPFParagraph paragraph = (XWPFParagraph)bodyElement;
    int paragraphIndex = getParagraphIndex(newDoc);
    newDoc.createParagraph();
    newDoc.setParagraph(paragraph,paragraphIndex);
}

/**
 * 获取段落组件的index值
 * @param newDoc
 * @return
 */
private static int getParagraphIndex(XWPFDocument newDoc){
    List<XWPFParagraph> paragraphs = newDoc.getParagraphs();
    if(CollectionUtils.isEmpty(paragraphs)){
        return 0;
    }
    return paragraphs.size();
}

/**
 * copy 表格组件
 * @param newDoc
 * @param bodyElement
 */
private static void setTable(XWPFDocument newDoc,IBodyElement bodyElement){
    XWPFTable table = (XWPFTable)bodyElement;
    int tableIndex = getTableIndex(newDoc);
    newDoc.createTable();
    newDoc.setTable(tableIndex,table);
}

/**
 * 获取表格组件的index值
 * @param newDoc
 * @return
 */
private static int getTableIndex(XWPFDocument newDoc){
    List<XWPFTable> tables = newDoc.getTables();
    if(CollectionUtils.isEmpty(tables)){
        return 0;
    }
    return tables.size();
}

/**
 * docx 写出到本地
 * @param newDoc
 * @param path
 */
public static void writeOutFile(XWPFDocument newDoc,String path){
    try {
        FileOutputStream out = new FileOutputStream(path);
        newDoc.write(out);
    }catch (IOException e){

    }
}

}

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值