1

我正在尝试在 java 中打开 MS Word 2003 文档,搜索指定的字符串并将其替换为新字符串。我使用 APACHE POI 来做到这一点。我的代码如下所示:

public void searchAndReplace(String inputFilename, String outputFilename,
            HashMap<String, String> replacements) {
    File outputFile = null;
    File inputFile = null;
    FileInputStream fileIStream = null;
    FileOutputStream fileOStream = null;
    BufferedInputStream bufIStream = null;
    BufferedOutputStream bufOStream = null;
    POIFSFileSystem fileSystem = null;
    HWPFDocument document = null;
    Range docRange = null;
    Paragraph paragraph = null;
    CharacterRun charRun = null;
    Set<String> keySet = null;
    Iterator<String> keySetIterator = null;
    int numParagraphs = 0;
    int numCharRuns = 0;
    String text = null;
    String key = null;
    String value = null;
        try {
            // Create an instance of the POIFSFileSystem class and
            // attach it to the Word document using an InputStream.
            inputFile = new File(inputFilename);
            fileIStream = new FileInputStream(inputFile);
            bufIStream = new BufferedInputStream(fileIStream);
            fileSystem = new POIFSFileSystem(bufIStream);
            document = new HWPFDocument(fileSystem);
            docRange = document.getRange();
            numParagraphs = docRange.numParagraphs();
            keySet = replacements.keySet();
            for (int i = 0; i < numParagraphs; i++) {
                paragraph = docRange.getParagraph(i);
                text = paragraph.text();
                numCharRuns = paragraph.numCharacterRuns();
                for (int j = 0; j < numCharRuns; j++) {
                    charRun = paragraph.getCharacterRun(j);
                    text = charRun.text();
                    System.out.println("Character Run text: " + text);
                    keySetIterator = keySet.iterator();
                    while (keySetIterator.hasNext()) {
                        key = keySetIterator.next();
                        if (text.contains(key)) {
                            value = replacements.get(key);
                            charRun.replaceText(key, value);
                            docRange = document.getRange();
                            paragraph = docRange.getParagraph(i);
                            charRun = paragraph.getCharacterRun(j);
                            text = charRun.text();
                        }
                    }
                }
            }
            bufIStream.close();
            bufIStream = null;
            outputFile = new File(outputFilename);
            fileOStream = new FileOutputStream(outputFile);
            bufOStream = new BufferedOutputStream(fileOStream);
            document.write(bufOStream);
        } catch (Exception ex) {
            System.out.println("Caught an: " + ex.getClass().getName());
            System.out.println("Message: " + ex.getMessage());
            System.out.println("Stacktrace follows.............");
            ex.printStackTrace(System.out);
        }
}

我用以下参数调用这个函数:

HashMap<String, String> replacements = new HashMap<String, String>();
replacements.put("AAA", "BBB");
searchAndReplace("C:/Test.doc", "C:/Test1.doc", replacements);

当 Test.doc 文件包含这样的简单行:“ AAA EEE ”时,它可以成功运行,但是当我使用复杂的文件时,它会成功读取内容并生成 Test1.doc 文件,但是当我尝试打开它时,它会给我以下错误:

Word 无法读取此文档。它可能是腐败的。尝试以下一项或多项操作: * 打开并修复文件。* 使用文本恢复转换器打开文件。(C:\Test1.doc)

请告诉我该怎么做,因为我是 POI 的初学者,我还没有找到一个好的教程。

4

5 回答 5

3

首先,您应该关闭文档。

除此之外,我建议您将原始 Word 文档重新保存为 Word XML 文档,然后手动将扩展名从 .XML 更改为 .doc 。然后查看您正在使用的实际文档的 XML 并跟踪内容以确保您不会意外编辑十六进制值(AAA 和 EEE 可能是其他字段中的十六进制值)。

如果没有看到实际的 Word 文档,很难说出发生了什么。

根本没有太多关于 POI 的文档,不幸的是对于 Word 文档。

于 2009-05-10T21:41:10.550 回答
2

我不知道:可以回答自己,但只是为了分享知识,我会回答自己。

浏览网页后,我找到的最终解决方案是:名为docx4j的库非常适合处理 MS docx 文件,虽然它的文档到现在还不够,它的论坛仍处于起步阶段,但总的来说它帮助我做我需要的..

感谢所有帮助我的4..

于 2009-05-20T08:40:14.043 回答
1

您可以尝试OpenOffice API,但没有很多资源可以告诉您如何使用它。

于 2009-05-11T07:15:06.883 回答
0

看起来可能是问题所在。

于 2009-05-11T06:53:26.263 回答
0

你也可以试试这个:http ://www.dancrintea.ro/doc-to-pdf/

于 2009-11-19T09:20:14.640 回答