将 Word 文档模板中的变量替换为 java

2022-09-02 03:23:24

我想加载一个模板Word文档以添加内容并另存为新文档。我正在处理.doc文件。

经过长时间的研究,我只找到了docx的解决方案:

http://www.smartjava.org/content/create-complex-word-docx-documents-programatically-docx4j

http://www.sambhashanam.com/mail-merge-in-java-for-microsoft-word-document-part-i/

因此,我想替换以这种格式编写的任何变量:通过其值。我可以用速度或Apache-poi做到这一点吗,最好的解决方案是什么。任何帮助将不胜感激。$VAR


答案 1

是的,您可以使用Apache-POI来做到这一点。变量名称必须是唯一的。请参阅以下代码

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class HWPFTest {
    public static void main(String[] args){
        String filePath = "F:\\Sample.doc";
        POIFSFileSystem fs = null;        
        try {            
            fs = new POIFSFileSystem(new FileInputStream(filePath));            
            HWPFDocument doc = new HWPFDocument(fs);
            doc = replaceText(doc, "$VAR", "MyValue1");
            saveWord(filePath, doc);
        }
        catch(FileNotFoundException e){
            e.printStackTrace();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    private static HWPFDocument replaceText(HWPFDocument doc, String findText, String replaceText){
        Range r1 = doc.getRange(); 

        for (int i = 0; i < r1.numSections(); ++i ) { 
            Section s = r1.getSection(i); 
            for (int x = 0; x < s.numParagraphs(); x++) { 
                Paragraph p = s.getParagraph(x); 
                for (int z = 0; z < p.numCharacterRuns(); z++) { 
                    CharacterRun run = p.getCharacterRun(z); 
                    String text = run.text();
                    if(text.contains(findText)) {
                        run.replaceText(findText, replaceText);
                    } 
                }
            }
        } 
        return doc;
    }

    private static void saveWord(String filePath, HWPFDocument doc) throws FileNotFoundException, IOException{
        FileOutputStream out = null;
        try{
            out = new FileOutputStream(filePath);
            doc.write(out);
        }
        finally{
            out.close();
        }
    }
}

答案 2

最近,我不得不解决同样的问题,但需要一个.docx文档。尝试上述方法导致以下错误(如本文所述):

org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的数据似乎在 Office 2007+ XML 中。您正在调用 POI 中处理 OLE2 Office 文档的部分。您需要调用POI的不同部分来处理此数据(例如XSSF而不是HSSF)

最后,我必须按如下方式更改代码(在我的情况下,.docx文件位于资源文件夹中):

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class XWPFTest {

    public static void main(String[] args) throws URISyntaxException, IOException {
        String resourcePath = "template.docx";
        Path templatePath = Paths.get(XWPFTest.class.getClassLoader().getResource(resourcePath).toURI());
        XWPFDocument doc =  new XWPFDocument(Files.newInputStream(templatePath));
        doc = replaceTextFor(doc, "UNIQUE_VAR", "MyValue1");
        saveWord("C:\\document.docx", doc);
    }

    private static XWPFDocument replaceTextFor(XWPFDocument doc, String findText, String replaceText){
        doc.getParagraphs().forEach(p ->{
            p.getRuns().forEach(run -> {
                String text = run.text();
                if(text.contains(findText)) {
                    run.setText(text.replace(findText, replaceText), 0);
                } 
            });
        });

        return doc;
    }

    private static void saveWord(String filePath, XWPFDocument doc) throws FileNotFoundException, IOException{
        FileOutputStream out = null;
        try{
            out = new FileOutputStream(filePath);
            doc.write(out);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally{
            out.close();
        }
    }

}

P.S. 我不得不删除 $ ,因为在.docx被管理是单独的运行,所以我不得不选择一个唯一的 var 名称的方法。我需要以下Apache POI依赖项:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>

推荐