JAVA自定义word文档中的参数并下载导出

项目场景:

有5个病例模板,模板是固定的,只需要更改其中的参数并且下载


直接上代码

<!--导出word-->
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
    <version>2.0.2</version>
</dependency>

  public ResultUtil<Map<String, String>> export1(MedicalRecordIdReq recordIdReq,HttpServletResponse response) throws Exception {
        HashMap<String, String> map = MapUtil.newHashMap(StatusCodeUtil.NUMBER_ONE);
        recordIdReq = AESUtil.decryptToObj(recordIdReq.getData(), MedicalRecordIdReq.class);
        MedicalRecord medicalRecord = medicalRecordService.getById(recordIdReq.getId());
        //模板位置
        String fileName = System.getProperty("os.name").toLowerCase().contains("win") ? "D:/" : "/www/usr/words/";
        //生成文件位置
        String newFileName = System.getProperty("os.name").toLowerCase().contains("win") ?"D:/words/"+recordIdReq.getId()+".docx" : "/www/usr/words/words/"+recordIdReq.getId()+".docx";

        try {
            //输出文档路径及名称
            File outFile = new File(newFileName);
            //Configuration 用于读取ftl文件
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");
            configuration.setDirectoryForTemplateLoading(new File(fileName));//ftl文件目录
            Map<String, Object> dataMap = new HashMap<String, Object>();
           
                dataMap.put("dentalPosition", medicalRecord.getDentalPosition());
                dataMap.put("implantBrandAndType", medicalRecord.getImplantBrandAndType());
                dataMap.put("mCode", medicalRecord.getMCode());
                dataMap.put("dCode", medicalRecord.getDCode());
                dataMap.put("bCode", medicalRecord.getBCode());
                dataMap.put("lCode", medicalRecord.getLCode());
                dataMap.put("nCode", medicalRecord.getNCode());
                dataMap.put("model", medicalRecord.getMCode());
                //以utf-8的编码读取ftl文件
                Template template = configuration.getTemplate("model5.ftl", "utf-8");
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
                template.process(dataMap, out);
                out.close();
          
            File file = new File(newFileName);
            MultipartFile multipartFile = getMultipartFile(file);
             //这里将本地文件上传到了七牛云获取外链
            String path = QiNiuUtil.customUpload1(2, QiNiuUtil.accessKey, QiNiuUtil.secretKey, QiNiuUtil.bucket, QiNiuUtil.domain, multipartFile);
            map.put("path", path);
            System.out.println(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.setEncryptData(map);
    }

总结

方法是很多的,希望大家多多分享

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值