java poi 替换word2007中的指定文本

开头贴出参考文章地址:

http://blog.sina.com.cn/s/blog_885585cb0101gnz7.html

http://www.cnblogs.com/dreammyle/p/5159267.html

效果图:原docx文件

效果图:替换后的docx文件


maven依赖:

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

java代码:

package com.smh.test;

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

import java.io.*;
import java.util.*;
import java.util.Map.Entry;


public class WordUtil {

    public static void main(String[] args) throws IOException {

        String srcPath = "D:\\a.docx";
        String destPath = "D:\\a-" + System.currentTimeMillis() + ".docx";

        InputStream in = new FileInputStream(srcPath);
        FileOutputStream out = new FileOutputStream(destPath);
        Map<String, String> map = new HashMap<>();
        map.put("${AGE}", "10777");
        map.put("${NAME}", "99999");

        replaceText(in, out, map);
        in.close();
        out.close();

    }

    public static void replaceText(InputStream inputStream, OutputStream outputStream, Map<String, String> map) {
        try {
            XWPFDocument document;//= new XWPFDocument(POIXMLDocument.openPackage(srcPath));
            document = new XWPFDocument(inputStream);
            //1. 替换段落中的指定文字
            Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
            String text;
            Set<String> set;
            XWPFParagraph paragraph;
            List<XWPFRun> run;
            String key;
            while (itPara.hasNext()) {
                paragraph = itPara.next();
                set = map.keySet();
                Iterator<String> iterator = set.iterator();
                while (iterator.hasNext()) {
                    key = iterator.next();
                    run = paragraph.getRuns();
                    for (int i = 0, runSie = run.size(); i < runSie; i++) {
                        text = run.get(i).getText(run.get(i).getTextPosition());
                        if (text != null && text.equals(key)) {
                            run.get(i).setText(map.get(key), 0);
                        }
                    }
                }
            }
            //2. 替换表格中的指定文字
            Iterator<XWPFTable> itTable = document.getTablesIterator();
            XWPFTable table;
            int rowsCount;
            while (itTable.hasNext()) {
                table = itTable.next();
                rowsCount = table.getNumberOfRows();
                for (int i = 0; i < rowsCount; i++) {
                    XWPFTableRow row = table.getRow(i);
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (XWPFTableCell cell : cells) {
                        for (Entry<String, String> e : map.entrySet()) {
                            if (cell.getText().equals(e.getKey())) {
                                cell.removeParagraph(0);
                                cell.setText(e.getValue());
                            }
                        }
                    }
                }
            }
            //3.输出流
            document.write(outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用Java POI库读取Word文档,您可以按照以下步骤进行操作: 1. 首先,您需要在项目导入POI的依赖。如果使用Maven进行项目管理,可以在pom.xml文件添加以下依赖项: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 2. 创建一个`File`对象,指定要读取的Word文档的路径。 ```java File file = new File("path/to/your/word/document.docx"); ``` 3. 使用`FileInputStream`读取Word文档。 ```java FileInputStream fis = new FileInputStream(file); ``` 4. 创建一个`XWPFDocument`对象,用于表示Word文档。 ```java XWPFDocument document = new XWPFDocument(fis); ``` 5. 遍历文档的段落和表格,并提取所需的内容。 ```java List<XWPFParagraph> paragraphs = document.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { // 处理段落内容 String text = paragraph.getText(); // 进一步处理文本内容... } List<XWPFTable> tables = document.getTables(); for (XWPFTable table : tables) { // 处理表格内容 List<XWPFTableRow> rows = table.getRows(); for (XWPFTableRow row : rows) { // 处理行内容 List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { // 处理单元格内容 String text = cell.getText(); // 进一步处理文本内容... } } } ``` 6. 最后,记得关闭文件流。 ```java fis.close(); ``` 通过以上步骤,您可以使用Java POI库读取Word文档的内容。请注意,POI库也可以读取旧版的`.doc`格式的Word文档,只需要将上述代码的`XWPFDocument`替换为`HWPFDocument`即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值