如何使用 Java 编辑 MS Word 文档? [英] How to edit MS Word documents using Java?

查看:26
本文介绍了如何使用 Java 编辑 MS Word 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Word 模板很少,我的要求是使用 Java 根据用户输入替换文档中的一些单词/占位符.我尝试了很多库,包括 2-3 个版本的 docx4j,但都没有效果,它们都没有做任何事情!

I do have few Word templates, and my requirement is to replace some of the words/place holders in the document based on the user input, using Java. I tried lot of libraries including 2-3 versions of docx4j but nothing work well, they all just didn't do anything!

我知道以前有人问过这个问题,但我尝试了我知道的所有选项.那么,使用什么 java 库我可以真正"替换/编辑这些模板?我更喜欢易于使用/代码行少"类型库.

I know this question has been asked before, but I tried all options I know. So, using what java library I can "really" replace/edit these templates? My preference goes to the "easy to use / Few line of codes" type libraries.

我使用的是 Java 8,我的 MS Word 模板在 MS Word 2007 中.

I am using Java 8 and my MS Word templates are in MS Word 2007.

更新

此代码使用SO成员Joop Eggen

public Main() throws URISyntaxException, IOException, ParserConfigurationException, SAXException
    {
        URI docxUri = new URI("C:/Users/Yohan/Desktop/yohan.docx");
        Map<String, String> zipProperties = new HashMap<>();
        zipProperties.put("encoding", "UTF-8");

         FileSystem zipFS = FileSystems.newFileSystem(docxUri, zipProperties);

           Path documentXmlPath = zipFS.getPath("/word/document.xml");

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();

            Document doc = builder.parse(Files.newInputStream(documentXmlPath));

            byte[] content = Files.readAllBytes(documentXmlPath);
            String xml = new String(content, StandardCharsets.UTF_8);
            //xml = xml.replace("#DATE#", "2014-09-24");
            xml = xml.replace("#NAME#", StringEscapeUtils.escapeXml("Sniper"));

            content = xml.getBytes(StandardCharsets.UTF_8);
            Files.write(documentXmlPath, content);
    }

但是这会返回以下错误

java.nio.file.ProviderNotFoundException: Provider "C" Not found

at: java.nio.file.FileSystems.newFileSystem(FileSystems.java:341) at java.nio.file.FileSystems.newFileSystem(FileSystems.java:341)

at java.nio.fileFileSystems.newFileSystem(FileSystems.java:276)

推荐答案

一个可以用于 docx(带有 XML 和其他文件的 zip)Java zip 文件系统和 XML 或文本处理.

One may use for docx (a zip with XML and other files) a java zip file system and XML or text processing.

URI docxUri = ,,, // "jar:file:/C:/... .docx"
Map<String, String> zipProperties = new HashMap<>();
zipProperties.put("encoding", "UTF-8");
try (FileSystem zipFS = FileSystems.newFileSystem(docxUri, zipProperties)) {
    Path documentXmlPath = zipFS.getPath("/word/document.xml");

使用 XML 时:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document doc = builder.parse(Files.newInputStream(documentXmlPath));
    //Element root = doc.getDocumentElement();

然后您可以使用 XPath 查找位置,并再次写回 XML.

You can then use XPath to find the places, and write the XML back again.

甚至可能您不需要 XML 但可以替换占位符:

It even might be that you do not need XML but could replace place holders:

    byte[] content = Files.readAllBytes(documentXmlPath);
    String xml = new String(content, StandardCharsets.UTF_8);
    xml = xml.replace("#DATE#", "2014-09-24");
    xml = xml.replace("#NAME#", StringEscapeUtils.escapeXml("Sniper")));
    ...
    content = xml.getBytes(StandardCharsets.UTF_8);
    Files.delete(documentXmlPath);
    Files.write(documentXmlPath, content);

为了快速开发,请将 .docx 的副本重命名为具有 .zip 文件扩展名的名称,然后检查文件.

For a fast development, rename a copy of the .docx to a name with the .zip file extension, and inspect the files.

File.write 应该已经应用 StandardOpenOption.TRUNCATE_EXISTING,但我添加了 Files.delete 因为发生了一些错误.查看评论.

File.write should already apply StandardOpenOption.TRUNCATE_EXISTING, but I have added Files.delete as some error occured. See comments.

这篇关于如何使用 Java 编辑 MS Word 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆