Could not initialize class . (freemarker生成的word文档在app端无法打开)

时间:2025-05-16 06:59:11

app下载项目中的word文档后打不开, freemarker根据xml模板生成的word的文件类型是Microsoft Office Word 97 - 2003 文档的。
自己创建的word在手机上是可以打开的。
模板本身就是XML格式文件,freemarker使用的方式是用类型字符串替换的方式,替换掉XML里面的字符然后生成按相同格式生成文件,然后后缀名定为.doc而已。
由于XML文件的头部有<?mso-application progid=""?>这样的字符串,所以电脑上的office word读到这个信息后知道按转换xml里标签转换成word的格式。
但手机上的word软件则没有这个功能,所以就打开失败。

maven中引入

<dependency>
    	<groupId></groupId>
    	<artifactId>jacob</artifactId>
    	<version>1.18</version>
	</dependency>

下载jacob-1.

/download/forlovehuan/10587731

import ;
import ;
import ;
import ;

public static String createNewWord() {
	ActiveXComponent _app = new ActiveXComponent("");
		        _app.setProperty("Visible", Variant.VT_FALSE);
	 Dispatch documents = _app.getProperty("Documents").toDispatch();
	 // 打开FreeMarker生成的Word文档
	Dispatch doc = (documents, "Open","原文件路径/", Variant.VT_FALSE, Variant.VT_TRUE).toDispatch();
		       // 另存为新的Word文档
		        (doc, "SaveAs", "新文件路径/", Variant.VT_FALSE, Variant.VT_TRUE);
	
		        (doc, "Close", Variant.VT_FALSE);
		        _app.invoke("Quit", new Variant[] {});
		        ();	
}

运行时报错

 : no jacob-1.18-x64 in 

需下载jacob-1.

/download/forlovehuan/10586055

放置在jdk/bin目录下,重新启动项目即可。之后依然报错,尝试之后发现放错位置了,应该放到jdk/jre/bin下。

项目发布在服务器后,下载依然报错:

 Caused by: : Could not initialize class .

确定以上没有问题,需在服务器安装office,问题解决。