jacob解决freemaker下载的word文档手机无法打开问题

java使用freemaker下载word的流程如下:

1.新建一个word模板

2.另存为xml格式

3.将xml后缀的文件后缀改成.ftl

4.将ftl中需要替换的内容使用freemaker jstl语法进行替换

5.使用如下代码进行下载 注意:后缀一定得是.doc

	public static File createWord(HttpServletResponse response,Map dataMap,String templateName,File outFile){
		File directory = new File("");
		String templateFolder = directory.getAbsolutePath();
        try {
            //创建配置实例
            Configuration configuration = new Configuration();
            //设置编码
            configuration.setDefaultEncoding("UTF-8");
            configuration.setEncoding(Locale.getDefault(), "utf-8");
            try {
	            configuration.setDirectoryForTemplateLoading(new File(templateFolder+"\\template\\"));
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
 
            //获取模板
            Template template = configuration.getTemplate(templateName);
            //将模板和数据模型合并生成文件
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
            //生成文件
            template.process(dataMap, out);
            //关闭流
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
       return outFile;
    }

使用以上逻辑已经可以满足项目正常需求,但是做移动端应用的时候发现手机移动端的office无法打开通过freemaker下载的word,而且有时直接显示的是xml字符串。

网上的解决方案要么版本不兼容,要么提示无法打开xml格式的文件,最终还是考虑使用windows指令将doc文件另存一下的方案,就选中了jacob插件,这也是jacob只能在windows上运行的原因。

为了使用该功能,网上下载了三个版本的jacob插件,最终测试后发现1.19版本可以正常使用,具体附件可以去我的资源里面查找。

文件结构如下:

使用方法是:

1.将jacob-1.10-x64.dll放入到jdk/bin目录下面,不要听网上的放到jre下面(当然开发环境是jdk,生产环境是jre,如果你是生产环境还是要放到jre下面的)

2.将jacob.jar注入到项目里面,因为我是springboot项目,所以我一般这么处理:

   2.1将jacob.jar注册到我本地仓库内

mvn install:install-file -Dfile=E:\jacob.jar -DgroupId=com.jacob -DartifactId=jacob -Dversion=1.19 -Dpackaging=jar

  2.2pom.xml引入依赖

<dependency>
			<groupId>com.jacob</groupId>
			<artifactId>jacob</artifactId>
			<version>1.19</version>
		</dependency>

3.写业务逻辑代码

public static File trans(File oldFile) {
		File directory = new File("");
		String templateFolder = directory.getAbsolutePath();
		String newPath =  templateFolder+"/temp/"+UUID.randomUUID().toString()+".doc";
		ActiveXComponent _app = new ActiveXComponent("KWPS.Application");
		_app.setProperty("Visible", com.jacob.com.Variant.VT_FALSE);

		Dispatch documents = _app.getProperty("Documents").toDispatch();
		
		// 打开FreeMarker生成的Word文档
		Dispatch doc = Dispatch.call(documents, "Open", oldFile.getPath(), com.jacob.com.Variant.VT_FALSE, com.jacob.com.Variant.VT_TRUE).toDispatch();
		// 另存为新的Word文档
		Dispatch.call(doc, "SaveAs",newPath, com.jacob.com.Variant.VT_FALSE, com.jacob.com.Variant.VT_TRUE);
		
		Dispatch.call(doc, "Close", com.jacob.com.Variant.VT_FALSE);
		_app.invoke("Quit", new com.jacob.com.Variant[] {});
		ComThread.Release();
		return new File(newPath);
	}

4.修改里面的一些细节,比如,我本地用的是wps所以我引用的是KWPS.Application,如果你使用的是office,那么相应的改为Word.Application。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用freemarker生成word ,并集成struts2 同时生成下载文档 资料附有Java源代码和自己总结的使用说明及注意事项 大至预览如下: 1、用word编辑好模板 普通字符串替换为 ${string} 表格循环用标签 姓名:${user.userName} , 性别:${user.sex} 2、将word模板另存为xml格式 3、将xml模板文件后缀名改为.ftl 4、编辑ftl文件 注意 编辑word模板时,${string} 标签最好是手动一次性输入完毕,或者使用记事本统一将整个${string}编辑好之后,粘贴至word里边。 也就是说,不要在word里首先打完 ${ } 之后,又从其它地方把 string 字符串粘贴至 { } 之间,这样在 word 转化为 xml时,解析会有问题freemarker解析时,会报错。 /** * @Desc:生成word文件 * @Author:张轮 * @Date:2014-1-22下午05:33:42 * @param dataMap word中需要展示的动态数据,用map集合来保存 * @param templateName word模板名称,例如:test.ftl * @param filePath 文件生成的目标路径,例如:D:/wordFile/ * @param fileName 生成的文件名称,例如:test.doc */ @SuppressWarnings("unchecked") public static void createWord(Map dataMap,String templateName,String filePath,String fileName){ try { //创建配置实例 Configuration configuration = new Configuration(); //设置编码 configuration.setDefaultEncoding("UTF-8"); //ftl模板文件统一放至 com.lun.template 包下面 configuration.setClassForTemplateLoading(WordUtil.class,"/com/lun/template/"); //获取模板 Template template = configuration.getTemplate(templateName); //输出文件 File outFile = new File(filePath+File.separator+fileName); //如果输出目标文件夹不存在,则创建 if (!outFile.getParentFile().exists()){ outFile.getParentFile().mkdirs(); } //将模板和数据模型合并生成文件 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8")); //生成文件 template.process(dataMap, out); //关闭流 out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值