我想在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 ”,它的工作原理成功,但是当我使用一个复杂的文件,将成功地读取内容,并生成测试1。 doc文件,但是当我尝试打开它,它将给我以下错误:

字符无法读取此文件。它可能已损坏。 请尝试以下的一种或多种: *打开并修复该文件。 *打开与文本恢复转换的文件。  (C:\ Test1.doc)

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

有帮助吗?

解决方案

您可以尝试 OpenOffice的API ,但;随行很多资源在那里告诉你如何使用它。

其他提示

首先,你应该关闭您的文档。

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

在没有看到实际的Word文档,很难说这是怎么回事。

有不是关于POI多文档可言,特别是对于不幸的Word文档。

我不知道:是它的确定回答自己,但只是为了分享知识,我会回答我自己

浏览网页后,最终溶液我发现是: 所谓的图书馆 docx4j 是处理MS的docx文件非常好虽然它的文档是不够至今及其论坛目前仍处于开始的步骤,但总体上帮我做什么,我需要..

感谢所有4谁帮我..

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

看起来像可能是问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top