1、下载jar包:
<!-- freemarker用于doc模板 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>jacob</artifactId>
<version>1.14.3</version>
</dependency>
2、配置:
在java_home中添加上jacob-1.14.3.dll
3、代码,使用word模板生成word文档,关键:xml转word, 不然手机打开是xml
代码:
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.smart.yx.dao.YxResumeMapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public String deal(Map<String,Object> dataMap) throws IOException {
boolean isLocal = true;
Writer out = null;
String rePath = null;
FileInputStream fileInputStream = null;
File outFile = null;
String name = (String) dataMap.get("name");
try {
//编号
// dataMap.put("name", "张大山");
//Configuration 用于读取ftl文件
Configuration configuration = new Configuration(new Version("2.3.2"));
configuration.setDefaultEncoding("utf-8");
/**
* 以下是两种指定ftl文件所在目录路径的方式,注意这两种方式都是
* 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
//指定路径的第二种方式,我的路径是C:/a.ftl
String path = isLocal ? htmlReadUtils.getPath() : "/usr/local/projects/";
configuration.setDirectoryForTemplateLoading(new File(path));
Template template = configuration.getTemplate("Template.ftl", "utf-8");
//输出文档路径及名称
String outPath = isLocal ? "d://" + name + "的简历.doc" : "/data/resume/" + name + "的简历.doc" ;
outFile = new File(outPath);
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
template.process(dataMap, out);
// doc转换
String targetPath = isLocal ? "d://test/" + name + "的简历.doc" : "/data/resume/test/" + name + "的简历.doc";
docToDocx(outPath, targetPath);
log.info("doc文档生成完成,开始进行上传到fastdfs");
File targetFile = new File(targetPath);
fileInputStream = new FileInputStream(targetFile);
String originalFilename = targetFile.getName().
substring(targetFile.getName().
lastIndexOf(".") + 1);
StorePath storePath = this.storageClient.uploadFile(
fileInputStream, fileInputStream.available(),originalFilename , null);
String fastDfsUrl = storePath.getFullPath();
String storeUrl = fastDfsUrl + "?attname=" + targetFile.getName();
log.info("上传文件拼接地址:{}", storeUrl);
String re = serverHttps + storeUrl;
log.info("上传文件拼接地址:{}", re);
rePath = re;
} catch (Exception e) {
log.error("错误:{}", e.getMessage());
e.printStackTrace();
} finally {
if(null != out){
out.close();
}
if(null != fileInputStream){
fileInputStream.close();
}
if(null != outFile){
// 删除本地文件
outFile.deleteOnExit();
}
}
return rePath;
}
/**
* xml形式的doc文件转换为Docx格式
* @param sourcePath 被转换文件的路径
* @param targetPath 目标文件路径
* @return
* @author lixs
* @Date 2018年5月29日16:24:08
*/
public static void docToDocx(String sourcePath, String targetPath){
//Word.Application代表COM OLE编程标识,可查询MSDN得到
ActiveXComponent app = new ActiveXComponent("Word.Application");
//设置Word不可见
app.setProperty("Visible",false);
//调用Application对象的Documents属性,获得Documents对象
Dispatch docs = app.getProperty("Documents").toDispatch();
//Dispatch doc = Dispatch.call(docs,"Open",sourcePath,new Variant(false),new Variant(true)).getDispatch();
Dispatch doc = Dispatch.call(docs,"Open",sourcePath).getDispatch();
Dispatch.call(doc,"SaveAS",targetPath,12);
//关闭打开的Word文件
Dispatch.call(doc,"Close",false);
//关闭Word应用程序
app.invoke("Quit",0);
}