java生成word文档,并转换doc、jacob-1.14.3操作记录

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);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值