zoukankan      html  css  js  c++  java
  • freemarker实现导出word复选框可点击效果

    记一次java导出word文档,导出的word文档里包含复选框并且能点击,一开始做了个输出字符的,比如这样:☑ □,然而并不能满足需求,网上找了一大堆也都是这种的。

    正文开始:

    先在word中添加复选框

      打开  开发工具-->设计模式

      添加复选框 

       

      添加完之后关闭  设计模式

      

       将word另存为   

      

      notepad++打开刚才的xml文件  切记不要格式化(过程中曾尝试不添加复选框格式化没问题,添加了复选框的这里如果格式化的话最后生成的word会打不开)

       

       

       添加  freeMarker语法

      

         <#if checkBox1=="true">

        <#else>

        </#if>

        checkBox1为java代码map中的key  eg:dataMap.put("checkBox1","true");

      添加完之后保存会提示个xml格式错误啥的不用管

      将xml文件后缀改为ftl放入java项目中

      java代码:

      

    try {
                //创建配置实例
                Configuration configuration = new Configuration();
                //设置编码
                configuration.setDefaultEncoding("UTF-8");
                configuration.setOutputFormat(XMLOutputFormat.INSTANCE);
                //ftl模板文件
                configuration.setClassForTemplateLoading(ProjectInfoChangeController.class, "/static/office/");
                //获取模板
                Template template = configuration.getTemplate("这里是刚才ftl格式的文件名.ftl","UTF-8");
                String fileName = "这里是输出的word名.doc";
                //输出文件
                File outFile = new File(ConstantsContext.getOfficeFileUploadPath() + 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();
                FileInputStream fileInputStream = new FileInputStream(ConstantsContext.getOfficeFileUploadPath() + fileName);
                return renderFile(fileName, fileInputStream);
            }catch (Exception e){
                log.error("导出*******发生异常,信息:{}",e);
            }
    

      详细代码其他博客都有很多

    WordMl格式浅析:https://www.doc88.com/p-0304343804150.html

    欢迎各位大佬指正。。。

  • 相关阅读:
    如何注册一个ens域名
    storj
    Polygon
    拜占庭容错
    10秒钟完成metamask上任意公链主网添加
    Logistic Regression
    Normal Equations
    神经网络学习笔记 lecture3:The backpropagation learning proccedure
    Three Types of Learning
    Exercise: Logistic Regression and Newton's Method
  • 原文地址:https://www.cnblogs.com/xuqiang7/p/13970335.html
Copyright © 2011-2022 走看看