java - 在 Java 中打开 Microsoft Word

标签 java apache ms-word apache-poi

我试图在 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 的初学者,我还没有找到好的教程。

最佳答案

首先您应该关闭文档。

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

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

关于 POI 的文档根本不多,不幸的是,尤其是 Word 文档。

关于java - 在 Java 中打开 Microsoft Word,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/846157/

相关文章:

ruby - Puppet 4.3.2 客户端节点无法通过 SSL 连接到 Puppet 服务器

java - 在进程、TCP、Java 之间传递套接字对象

linux - 为什么我的 Apache 只能访问 root 拥有的文件?

Java删除数组中的冗余项

Python无法导入ssl模块

css - word vba创建具有个性化风格的条目

excel - 当宏调用 Excel 打开时,Word 卡住

c# - Formfields.BookMarks.get_Item().Checkbox.Value 不起作用(自动化词)

java - 验证名称不能为空

Java DynamoDB——仅在键不存在时插入(没有映射器)