docx与doc替换文本与docx替换图片 poi

POI替换word文档文字,后台获取数据修改word文档中的数据 - 蕾姆大人 - 博客园 (cnblogs.com)icon-default.png?t=N7T8https://www.cnblogs.com/MyReM/p/9109919.html

docx与doc替换文本

docx替换摘自以上博客

添加依赖

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.0.0</version>
        </dependency>

方法

其中map中内容为 map.put("${title}","这是标题")

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.*;

import java.io.*;
import java.util.*;


public class PoiSgfaUtil {
    /**
     * 这个适合doc 文件替换传递不同参数
     * doc 文件替换内容
     * @param dataMap 替换的map
     * @param exportFile 源文件
     * @param ins 最后导出的文件
//     * @param substring 追加的内容
     * @return
     * @throws IOException
     */
    public static void generateAttorneySignatureFile(Map<String, String> dataMap, File exportFile, InputStream ins) throws IOException {
        HWPFDocument document = new HWPFDocument(ins);
        // 读取word的文本内容
        Range range = document.getRange();
        // 根据第二部加工的key进行替换
        for (Map.Entry<String, String> entry : dataMap.entrySet()) {
            range.replaceText("${" + entry.getKey() + "}", entry.getValue());
        }
        //写入新文件
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            document.write(byteArrayOutputStream);
            OutputStream outputStream = new FileOutputStream(exportFile);
            outputStream.write(byteArrayOutputStream.toByteArray());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return exportFile;
    }

    /**
     * @Author:张李三
     * 有事请联系我: 1994387xxxx 联系了也不接,谢谢
     * @Date: 2024/1/5 15:56
     * @Param: 主文件 替换之后的文件地址 替换的内容
     * @return:
     * 适用于docx文件替换内容
    */
    public static void generateAttorneySignatureFileDocx (String inputSrc, String outSrc, Map<String,String> map) throws Exception {
        FileInputStream fis = new FileInputStream(new File(inputSrc));
        XWPFDocument document = new XWPFDocument(fis);
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.getText(0);
                for(String key : map.keySet()){
                    if(text != null && text.equals(key)){
                        //替换的时候要注意,setText是有两个参数的
                        //第一个是替换的文本,第二个是从哪里开始替换
                        //0是替换全部,如果不设置那么默认就是从原文字
                        //结尾开始追加
                        run.setText(map.get(key),0);
                    }
                }
            }
        }
        /**
         * 替换表格中指定的文字
         */
        for(XWPFTable tab : document.getTables()){
            for(XWPFTableRow row : tab.getRows()){
                for(XWPFTableCell cell : row.getTableCells()){
                    //注意,getParagraphs一定不能漏掉
                    //因为一个表格里面可能会有多个需要替换的文字
                    //如果没有这个步骤那么文字会替换不了
                    for(XWPFParagraph p : cell.getParagraphs()){
                        for(XWPFRun r : p.getRuns()){
                            String text = r.getText(0);
                            for(String key : map.keySet()){
                                if(text.equals(key)){
                                    r.setText(map.get(text),0);
                                }
                            }
                        }
                    }
                }
            }
        }
        FileOutputStream fos = new FileOutputStream(new File(outSrc));
        document.write(fos);
        fos.close();
    }
}

docx文件替换图片

    public static void exportSimpleWord(String oldFileUrl) throws Exception {
        //替换图片
        Map<String,Object> one = new HashMap<String, Object>();
        one.put("width", 100);
        one.put("height", 150);
        one.put("type", "jpg");
        one.put("content", WordUtilS.inputStream2ByteArray(new             
        FileInputStream("D:\\xxx.png"), true));
        param.put("imgaqk",one);
        //输入和输出 这需要的是docx
        CustomXWPFDocument doc = WordUtilS.generateWord(param, oldFileUrl);
        // 最后输出为
        FileOutputStream fopts = new FileOutputStream("D:\\Code\\Resources\\456.docx");
        doc.write(fopts);
        fopts.close();
    }

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值