Java 访问 Word 文件 DOCX zip 和修改文档文件

Java Accessing Word file DOCX zip and modifying document file

我正在尝试使用提供的示例代码来使用简单的查找和替换。 这是在我的应用程序中的 Beanshell 中。我在指示的行上收到错误; 如果我使用 'env' 或 'Null' 会这样做! logit 行表明 'env' 已填充,并且来自表单的路径确实存在。 (使用网络共享的 UNC 路径)。

错误:"Typed variable declaration : Attempt to resolve method: newFileSystem() on undefined variable or class name: FileSystems : at Line: 46 : in file: inline evaluation of: FileSystems .newFileSystem ( docxUri , null )"

非常感谢帮助。

File zipfile = new File(thisProcess.getFieldValue("txt_Path"));
URI docxUri = new URI("jar:" + zipfile.toURI());

Map env = new HashMap();
importObject( env );
put("create", "false");
put("encoding", "UTF-8");

if(zipfile.exists() && !zipfile.isDirectory()) 
{ 
    logit.info("Document EXISTS");
}

logit.info(env);

try
{
    FileSystem zipFS = FileSystems.newFileSystem(docxUri, null); //ERROR ON THIS LINE
    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.delete(documentXmlPath);
    Files.write(documentXmlPath, content);
}

不确定我做错了什么,但没有一个回复。 我选择使用专家交流建议的 Docx4J!