使用POI替换word中的特定字符/文字改进版

package com.xfzx.test.POI.main;  

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;  

import org.apache.poi.POIXMLDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;  

public class WordPOI {  

    // 返回Docx中需要替换的特殊字符,没有重复项
    // 推荐传入正则表达式参数"\\$\\{[^{}]+\\}"
    public ArrayList<String> getReplaceElementsInWord(String filePath,
            String regex) {
        String[] p = filePath.split("\\.");
        if (p.length > 0) {// 判断文件有无扩展名
            // 比较文件扩展名
            if (p[p.length - 1].equalsIgnoreCase("doc")) {
                ArrayList<String> al = new ArrayList<>();
                File file = new File(filePath);
                HWPFDocument document = null;
                try {
                    InputStream is = new FileInputStream(file);
                    document = new HWPFDocument(is);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Range range = document.getRange();
                String rangeText = range.text();
                CharSequence cs = rangeText.subSequence(0, rangeText.length());
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(cs);
                int startPosition = 0;
                while (matcher.find(startPosition)) {
                    if (!al.contains(matcher.group())) {
                        al.add(matcher.group());
                    }
                    startPosition = matcher.end();
                }
                return al;
            } else if (p[p.length - 1].equalsIgnoreCase("docx")) {
                ArrayList<String> al = new ArrayList<>();
                XWPFDocument document = null;
                try {
                    document = new XWPFDocument(
                            POIXMLDocument.openPackage(filePath));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // 遍历段落
                Iterator<XWPFParagraph> itPara = document
                        .getParagraphsIterator();
                while (itPara.hasNext()) {
                    XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
                    String paragraphString = paragraph.getText();
                    CharSequence cs = paragraphString.subSequence(0,
                            paragraphString.length());
                    Pattern pattern = Pattern.compile(regex);
                    Matcher matcher = pattern.matcher(cs);
                    int startPosition = 0;
                    while (matcher.find(startPosition)) {
                        if (!al.contains(matcher.group())) {
                            al.add(matcher.group());
                        }
                        startPosition = matcher.end();
                    }
                }
                // 遍历表
                Iterator<XWPFTable> itTable = document.getTablesIterator();
                while (itTable.hasNext()) {
                    XWPFTable table = (XWPFTable) itTable.next();
                    int rcount = table.getNumberOfRows();
                    for (int i = 0; i < rcount; i++) {
                        XWPFTableRow row = table.getRow(i);
                        List<XWPFTableCell> cells = row.getTableCells();
                        for (XWPFTableCell cell : cells) {
                            String cellText = "";
                            cellText = cell.getText();
                            CharSequence cs = cellText.subSequence(0,
                                    cellText.length());
                            Pattern pattern = Pattern.compile(regex);
                            Matcher matcher = pattern.matcher(cs);
                            int startPosition = 0;
                            while (matcher.find(startPosition)) {
                                if (!al.contains(matcher.group())) {
                                    al.add(matcher.group());
                                }
                                startPosition = matcher.end();
                            }
                        }
                    }
                }
                return al;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
  /* 何问起 hovertree.com */
    // 替换word中需要替换的特殊字符
    public static boolean replaceAndGenerateWord(String srcPath,
            String destPath, Map<String, String> map) {
        String[] sp = srcPath.split("\\.");
        String[] dp = destPath.split("\\.");
        if ((sp.length > 0) && (dp.length > 0)) {// 判断文件有无扩展名
            // 比较文件扩展名
            if (sp[sp.length - 1].equalsIgnoreCase("docx")) {
                try {
                    XWPFDocument document = new XWPFDocument(
                            POIXMLDocument.openPackage(srcPath));
                    // 替换段落中的指定文字
                    Iterator<XWPFParagraph> itPara = document
                            .getParagraphsIterator();
                    while (itPara.hasNext()) {
                        XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
                        List<XWPFRun> runs = paragraph.getRuns();
                        for (int i = 0; i < runs.size(); i++) {
                            String oneparaString = runs.get(i).getText(
                                    runs.get(i).getTextPosition());
                            for (Map.Entry<String, String> entry : map
                                    .entrySet()) {
                                oneparaString = oneparaString.replace(
                                        entry.getKey(), entry.getValue());
                            }
                            runs.get(i).setText(oneparaString, 0);
                        }
                    }  

                    // 替换表格中的指定文字
                    Iterator<XWPFTable> itTable = document.getTablesIterator();
                    while (itTable.hasNext()) {
                        XWPFTable table = (XWPFTable) itTable.next();
                        int rcount = table.getNumberOfRows();
                        for (int i = 0; i < rcount; i++) {
                            XWPFTableRow row = table.getRow(i);
                            List<XWPFTableCell> cells = row.getTableCells();
                            for (XWPFTableCell cell : cells) {
                                String cellTextString = cell.getText();
                                for (Entry<String, String> e : map.entrySet()) {
                                    if (cellTextString.contains(e.getKey()))
                                        cellTextString = cellTextString
                                                .replace(e.getKey(),
                                                        e.getValue());
                                }
                                cell.removeParagraph(0);
                                cell.setText(cellTextString);
                            }
                        }
                    }
                    FileOutputStream outStream = null;
                    outStream = new FileOutputStream(destPath);
                    document.write(outStream);
                    outStream.close();
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }  

            } else
            // doc只能生成doc,如果生成docx会出错
            if ((sp[sp.length - 1].equalsIgnoreCase("doc"))
                    && (dp[dp.length - 1].equalsIgnoreCase("doc"))) {
                HWPFDocument document = null;
                try {
                    document = new HWPFDocument(new FileInputStream(srcPath));
                    Range range = document.getRange();
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                        range.replaceText(entry.getKey(), entry.getValue());
                    }
                    FileOutputStream outStream = null;
                    outStream = new FileOutputStream(destPath);
                    document.write(outStream);
                    outStream.close();
                    return true;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    return false;
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }  

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String filepathString = "D:/2.doc";
        String destpathString = "D:/2ttt.doc";
        Map<String, String> map = new HashMap<String, String>();
        map.put("${NAME}", "王五王五啊柯乐义的辣味回答侯何问起网");
        map.put("${NsAME}", "王五王五啊王力味回答侯何问起网");
        map.put("${NAMaE}", "王五王五啊柯乐义侯何问起网");
        map.put("${NArME}", "王五王五啊柯乐义的辣味回答东拉网");
        map.put("${NwAME}", "王五王五啊王的辣味回答侯何问起网");
        map.put("${NA4ME}", "王五王五啊王力侯何问起网");
        map.put("${N5AME}", "王五王五辣味回答侯何问起网");
        map.put("${NAadwME}", "王五力宏的辣味回答侯何问起网");
        System.out.println(replaceAndGenerateWord(filepathString,
                destpathString, map));
    }
}  

推荐:http://www.cnblogs.com/roucheng/p/3504465.html

时间: 2024-10-18 22:50:37

使用POI替换word中的特定字符/文字改进版的相关文章

JavaScript替换字符串中最后一个字符

1.问题背景 在一个输入框中,限制字符串长度为12位.利用键盘输入一个数字,会将字符串中最后一位替换,比方:111111111111.再输入一个3,会显示111111111113 2.详细实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html x

Javascript --扩展String实现替换字符串中index处字符

String.prototype.replaceCharAt = function(n,c){ return this.substr(0, n)+ c + this.substr(n+1,this.length-1-n); } Javascript --扩展String实现替换字符串中index处字符

.net 下word 中的图片与文字分离

最近在做一个项目要求word 中的图片与文字分离 ,找了好久终于找到一个完美的方法 c#实现word中的图文分离 part 1: class define Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->publicclass WordSeparator:IDisposable { #region Constructor public WordS

WORD中字数和字符

在WORD中,一个汉字算1个字符,也算是1个字,一个标点符号也算1个字符,也算是1个字,WORD中字符数的统计分为(不计空格)和(计空格)的两种. 如果一篇文章仅由汉字和标点符号组成,那么字数=字符数(不计空格)=汉字数+标点符号数. 一连串的英语字母或者数字,再长都只算是1个字,但字符数却是按照字母或者数字的个数计算的. 例如: Single Dog 2017都脱单 字数:6个 字符数:16个(不计空格)

在字符串中删除特定字符

63.在字符串中删除特定的字符.题目:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符.例如,输入”They are students.”和”aeiou”, 则删除之后的第一个字符串变成”Thy r stdnts.”. 思路: 1. 位图法 将两个字符串分别转换成bitmap 然后对他们做异或xor运算,得到的结果即为排除了第二个字符串的所有字符, 然后对该结果依次与原字符串的所有字符进行与运算,结果不为零的即为所得    恩 位图真是个好东西啊...时间复杂度o(n+m) 花在了遍历

c之PAT刷体--字符串-01--从字符串中找到特定字符

字符串-01. 在字符串中查找指定字符(15) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 白洪欢(浙江大学) 输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出"Not found":若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例

替换xml中某些特定的elements

替换xml中某些node, 如<name>zhangsan</name>想替换成<name>lisi</name>: package strip; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; impo

读取Json,并替换json中的指定字符

string jsonfile = @"E:\history.json";//JSON文件路径 using (System.IO.FileStream file = new FileStream(jsonfile, FileMode.Open, FileAccess.ReadWrite)) { var buffer = new byte[file.Length];//获取用字节表示的流长度 file.Read(buffer, 0, buffer.Length);//0 字节 1 偏移量

替换word中的数据,并给导入word的图片添加水印

public static void ExportWord(string tempFilePath, string outPath, Dictionary<string, string> data, Dictionary<string, string> imageList) { using (FileStream stream = File.OpenRead(tempFilePath)) { XWPFDocument doc = new XWPFDocument(stream);