Java操作word指定位置插入数据

需求背景:因项目需要无法使用poi-tl包利用{{}}定位的方式向word模板的指定位置填写信息
解决方法:现利用替换占位字段的方式,在要替换的地方前后都插入书签
示例word:
在需要替换填写内容的地方放置占位标识并在该标识前后添加书签
在这里插入图片描述

代码示例:

/**
 * @author YongXin
 * @date Created in 2022/7/18 20:57
 */
public class InsertWordTest {
    public static void main(String[] args) throws IOException {
        Map<String,String> map = new HashMap<>();
        map.put("mj","内部");
        map.put("dept","办公室");
        map.put("b","小明,小红");
        map.put("h","董事会");
        insertAndOutFile("C:/Users/96522/Desktop/template.docx","C:/Users/96522/Desktop/out.docx",map);
    }
    /**
     *将word中某些标签替换成指定的值,并生成一个新的word文档。
     * @param templateFilePath word模板文件路径
     * @param outFilePath 填充后输出文件路径
     * @param map  key:word中的占位标签,value对应标签要替换的值。
     * @throws IOException
     */
    public static void insertAndOutFile(String templateFilePath, String outFilePath, Map<String,String> map) throws IOException {
        //准备工作,生成docx对象
        String templatePath=templateFilePath;
        InputStream is=new FileInputStream(templatePath);
        XWPFDocument docx=new XWPFDocument(is);
        //获取表格
        List<XWPFTable> tables=docx.getTables();
        //定位到第一个表格
        XWPFTable table=tables.get(0);
        //遍历该表格所有的行
        for(int i=0;i<table.getRows().size();i++) {
            XWPFTableRow row=table.getRow(i);
            //遍历该行所有的列
            for(int j=0;j<row.getTableCells().size();j++) {
                XWPFTableCell cell=row.getTableCells().get(j);
                //获取该格子里所有的段
                List<XWPFParagraph> paragraphs=cell.getParagraphs();
                for(XWPFParagraph p:paragraphs) {
                    //遍历该格子里的段
                    List<XWPFRun> runs=p.getRuns();
                    for(XWPFRun run:runs) {
                        //遍历该段里的所有文本
                        String str=run.toString();
                        //如果该段文本包含map中的key,则替换为map中的value值。
                        Set<String> keySet = map.keySet();
                        for(String key:keySet){
                            if(str.trim().equals(key)){
                                //替换该文本0位置的数据。
                                run.setText(map.get(key),0);

                            }
                        }
                    }
                }
            }
        }
        //输出
        OutputStream os=new FileOutputStream(outFilePath);
        docx.write(os);
        is.close();
        os.close();
    }
}

依赖:

<dependencies>
        <!-- word工具 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

    </dependencies>

结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值