FreeMark 填充World

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了FreeMark 填充World脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

FreeMaker模板填充Word下载

  • Word工具类
/**
 * Word导出工具类.
 *
 * @author Pang 2020-10-23
 */
public class WordUtil {

    /**
     * 生成word文档.
     *
     * @param dataMap      填充数据
     * @param templateName 模板名称
     * @return 文件
     */
    public static File createWord(Map dataMap, String templateName) {

        try {
            //创建配置实例
            configuration configuration = new Configuration(Configuration.getVersion());

            //设置编码
            configuration.setDefaultEncoding("UTF-8");

            //ftl模板文件
            configuration.setTemplateLoader(new ClassTemplateLoader(WordUtil.class, "/template/"));

            //获取模板
            Template template = configuration.getTemplate(templateName);

            //输出临时文件
            File outFile = File.createTempFile("temp", ".doc");

            //将模板和数据模型合并生成文件
            WrITer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
            //生成文件
            template.PRocess(dataMap, out);
            //关闭流
            out.flush();
            out.close();
            return outFile;
        } catch (Exception exception) {
            exception.printStackTrace();
            return null;
        }
    }

    /**
     * 导出文件.
     *
     * @param response     响应
     * @param map          填充的数据
     * @param templateName 模板名称
     * @throws IOException 读写异常
     */
    public static void exportMillCertificateWord(HttpServletResponse response,
                                                 Map map, String templateName) throws IOException {
        File file = null;
        InputStream fin = null;
        ServletOutputStream out = null;
        try {
            String fileName = "下载文件名" + System.currentTimeMillis() + ".doc";
            file = WordUtil.createWord(map, templateName);
            fin = new FileinputStream(file);
            response.setCharacterEncoding("utf-8");
            response.setContentTyPE("application/msword");
            response.setHeader("Content-Disposition", "attachment;filename*=utf-8''"
                    .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
            out = response.getOutputStream();
            // 缓冲区
            byte[] buffer = new byte[1024];
            int byteStoread = -1;
            // 通过循环将读入的Word文件的内容输出到浏览器中
            while ((bytestoRead = fin.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
        } finally {
            if (fin != null) {
                fin.close();
            }
            if (out != null) {
                out.close();
            }
            if (file != null) {
                // 删除临时文件
                file.deleteOnExit();
            }
        }

    }

  • Word填充数据工具类

    /**
     * 申请信息word模板填充数据工具类.
     *
     * @author Pang 2020-10-24
     */
    public class WordDataUtil implements Serializable {
    
        /**
         * 向word模板填充数据.
         *
         * @param wordVo word导出信息实体类
         * @return 数据集
         */
        public static Map<String, Object> setDataToWord(WordVO wordVo) {
            converEmptyNullToString(applyWordVO);
            HashMap<String, Object> dataMap = new HashMap<>();
            dataMap.put("companyName", applyWordVO.getCompanyName());
            dataMap.put("companyAddress", applyWordVO.getCompanyAddress());
    
            return dataMap;
        }
    
        /**
         * 对象转换 将null转换成"" .
         *
         * @param object 对象
         */
        public static void converEmptyNullToString(Object object) {
    
            try {
                //利用反射获取类的所有属性
                Field[] fs = object.getClass().getDeclareDFields();
                for (int i = 0; i < fs.length; i++) {
                    Field f = fs[i];
                    //设置属性可以访问
                    f.setAccessible(true);
                    Object val = f.get(object);
                    //得到此属性的类型
                    String type = f.getType().toString();
                    if (type.endsWith("String")) {
                        if (!"" .equals(val) && null == val) {
                            f.set(object, "");
                        }
                    }
                }
            } catch (SecurityException exception) {
                exception.printStackTrace();
            } catch (IllegalargumentException exception) {
                exception.printStackTrace();
            } catch (IllegalAccessException exception) {
                exception.printStackTrace();
            }
    
        }
    

脚本宝典总结

以上是脚本宝典为你收集整理的FreeMark 填充World全部内容,希望文章能够帮你解决FreeMark 填充World所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。