如何在替换文本后将doc文件内容保留为相同的格式

时间:2013-09-16 10:59:59

标签: java file io ms-word apache-poi

我有一个doc文件。我正在使用Java程序将“location”替换为“USA”。但是doc文件失去了它的格式。

这是我的代码:

replaceText(String path, String fileName, String text, HttpServletRequest request) {

    String newFileName = null;
    POIFSFileSystem fs = null;
    try
    {
        URL url = new URL(path+"/doc/"+fileName);
        InputStream in = url.openStream();
        fs = new POIFSFileSystem(in);
        HWPFDocument doc = new HWPFDocument(fs);

        WordExtractor we = new WordExtractor(doc);

        newFileName = request.getRealPath("") + "/doc/"+text+".doc";
        OutputStream writer= new FileOutputStream(newFileName);
        String[] paragraphs = we.getParagraphText();

        for( int i=0; i<paragraphs .length; i++ ) {
            paragraphs[i] = paragraphs[i].replaceAll("location",text);
            byte[] contentInBytes = paragraphs[i].getBytes();
            writer.write(contentInBytes);
        }
        writer.close();

    }
    catch(Exception e) {
        e.printStackTrace();
    }
    return newFileName;
}

1 个答案:

答案 0 :(得分:1)

最后,我找到了在不丢失格式的情况下替换文本的方法。 我使用了Apache POI

这些是jar文件:

poi-3.9-20121203.jarpoi-scratchpad-3.9-20121203.jar

以下是代码:

 try
{
    URL url = new URL("localhost:8080/testproject/downloads/fileName.doc");
    InputStream in = url.openStream();
    fs = new POIFSFileSystem(in);
    HWPFDocument doc = new HWPFDocument(fs);
    newFileName = request.getRealPath("") + "/downloads/newFile.doc";
    OutputStream writer = new FileOutputStream(newFileName);

    doc.getRange().replaceText("Location", "USA");
    doc.write(writer);
    writer.close();
}
catch(Exception e) 
{
    e.printStackTrace();
}