利用Uedior做模板生成word文档

利用Uedior做模板生成word文档

新建模板时选择不同的模板类型出现不同的模板参数

 

不同的模板类型出现不同的模板参数代码如下

switch (type) {
   
case 1:
      
UE.delEditor('editor');
       
ueditor = UE.getEditor('editor', {
           
toolbars: [
                [
                   
'newaddbtn',
                   
'fullscreen', 'source', 'undo', 'redo', 'bold', 'italic',
                   
'underline', 'fontborder', 'backcolor', 'fontsize', 'fontfamily',
                   
'justifyleft', 'justifyright', 'justifycenter', 'justifyjustify',
                    
'strikethrough', 'superscript', 'subscript', 'removeformat',
                   
'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
                   
'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist',
                   
'selectall', 'cleardoc', 'link', 'unlink', 'simpleupload', 'insertimage', 'emotion', 'help','insertcode'
               
]],
           
initialFrameWidth: 640,
           
initialFrameHeight: 300,
        });
       
break;
   
case 2:
       
UE.delEditor('editor');
       
ueditor = UE.getEditor('editor', {
           
toolbars: [
                [
                   
'newaddbtn',
                   
'fullscreen', 'source', 'undo', 'redo', 'bold', 'italic',
                   
'underline', 'fontborder', 'backcolor', 'fontsize', 'fontfamily',
                   
'justifyleft', 'justifyright', 'justifycenter', 'justifyjustify',
                   
'strikethrough', 'superscript', 'subscript', 'removeformat',
                   
'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
                   
'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist',
                   
'selectall', 'cleardoc', 'link', 'unlink', 'simpleupload', 'insertimage', 'emotion', 'help','insertcode'
               
]],
           
initialFrameWidth: 640,
           
initialFrameHeight: 300,
        });
       
break;
   
case 3:
       
UE.delEditor('editor');//先删除初始化的插件,再新增
        ueditor = UE.getEditor('editor', {
           
toolbars: [
                [
                   
'reddoc_name','reddoc_riskcount','reddoc_riskname','reddoc_websitename','reddoc_noticehanno','reddoc_noticetime','createtime','createperson','cellphone',
                   
'fullscreen', 'source', 'undo', 'redo', 'bold', 'italic',
                   
'underline', 'fontborder', 'backcolor', 'fontsize', 'fontfamily',
                   
'justifyleft', 'justifyright', 'justifycenter', 'justifyjustify',
                   
'strikethrough', 'superscript', 'subscript', 'removeformat',
                   
'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
                   
'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist',
                   
'selectall', 'cleardoc', 'link', 'unlink', 'simpleupload', 'insertimage', 'emotion', 'help','insertcode'
               
]],
           
initialFrameWidth: 640,
           
initialFrameHeight: 300,
        });
       
break;

}

 

 

自定义uedior工具

初始化

var ueditor=""

ueditor ueditor = UE.getEditor('editor', {

    toolbars: [

        [

            'newaddbtn'
        ]],

    initialFrameWidth: 640,

    initialFrameHeight: 300,

});

配置文件初始化按钮(uedior.config.js)

 

添加按钮事件(uedior.all.js)

触发事件命令初始化

 

做一个插入文本操作

  UE.commands['newaddbtn'] = {

  

    execCommand : function(cmd){

  

        this.execCommand('inserthtml', '{{'+cmd+'}}'); return true;

    },

    queryCommandState:function(){

  

    }

};

结果{{newaddbtn}}}

 

给按钮添加样式(themes/default/css/ueditor.css)

.edui-default .edui-toolbar .edui-for-newaddbtn .edui-icon{

    background-position: -72px -34px;

  background: url(../images/paramIcon.png) no-repeat;//自定义按钮背景

}

 

按钮鼠标略过文字提醒(lang/zh-cn/zn-cn.js)

 

调用模板,生成文件word文件

后台获取新增模板内容,过滤{{}}}获取到参数集合

//查找参数列表,body为模板html内容

  public List<String> buildReultMap(String body) {

    String[] ar = body.split("}}");

    List<String> list = new LinkedList<>();

    if (ar.length > 0) {

        for (int i = 0; i < ar.length - 1; i++) {

            String s = ar[i];

            String key = s.substring(s.indexOf("{{") + 2);

            list.add(key);

        }

    }

  

    return list;

}

 

 

设置参数对应的参数值,回填到html中

    /**

     * @param body

     * @param map 参数对应参数值

    * @param path 文件输出路径

     */

    public String createWord(String body, Map<String, Object> map, String path) {

  

  //        InputStream cssIs2 = new FileInputStream("f:\\ueditor.css");

//        InputStream cssIs1 = new FileInputStream("f:\\codemirror.css");

        //String body = this.getContent(bodyIs);

        String[] ar = body.split("}}");

        StringBuffer stringBuffer = new StringBuffer();

        if (ar.length > 0) {

            for (int i = 0; i < ar.length - 1; i++) {

                String s = ar[i];

                if (s.indexOf("{{")!=-1) {

                    String key = s.substring(s.indexOf("{{") + 2);

                    //a为参数key,获取key对应的value

                    Object val = map.get(key);

                    //找到对应值进行替换

                    stringBuffer.append(s.replace("{{" + key, val.toString()));

                }else{

                    stringBuffer.append(s);

                }

            }

        }

        //拼一个标准的HTML格式文档

        String content = "<html><head><body>" + stringBuffer.toString() + "</body></html>";

        String flag = "";

        try {

            InputStream is = new ByteArrayInputStream(content.getBytes("GBK"));

            OutputStream os = new FileOutputStream(path);

            this.inputStreamToWord(is, os);

            flag = "SUCCESS";

        } catch (Exception e) {

            flag = "FAILD";

            logger.error("模板转word文档时发生异常:" + e, e);

        } finally {

            return flag;

        }

  

    }

 

 

Html转word

private void inputStreamToWord(InputStream is, OutputStream os) throws IOException {

  

    POIFSFileSystem fs = new POIFSFileSystem();

    //对应于org.apache.poi.hdf.extractor.WordDocument

    fs.createDocument(is, "WordDocument");

    fs.writeFilesystem(os);

    os.close();

    is.close();

}

 

参数值可利用sql管理动态获取值

有一个存放sql的位置,sql格式如下,{{}}里为参数名

select * from 表名 where tid={{tid}}

 

获取参数名,并设置参数值,拼接sql语句

//拼接sql语句,mapkey(参数名),val(参数值)

  String[] ar = sql.split("}}");

  if (ar.length > 0) {

    for (int i = 0; i < ar.length; i++) {

        String s = ar[i];

        if (s.indexOf("{{")!=-1) {

            String key = s.substring(s.indexOf("{{") + 2);

            //a为参数key,获取key对应的value

            Object val = map.get(key);

            //找到对应值进行替换

            stringBuffer.append(s.replace("{{" + key, val.toString()));

        }else{

            stringBuffer.append(s);

        }

    }

    

}

sql=stringBuffer.toString();

 

执行mysql

//mysql执行

  List<Map> query = new ArrayList<>();

  try {

    query = Db.use(dataSource).query(sql, Map.class);

} catch (Exception e) {

    logger.error("执行模板sql时发生异常:" + e, e);

} finally {

    return query;

}

 

所需pom.xml(可能有多余,自行删减)

<!--百度富文本 -->

  <dependency>

    <groupId>cn.songxinqiang</groupId>

    <artifactId>com.baidu.ueditor</artifactId>

    <version>1.1.2-offical</version>

  </dependency>

  <dependency>

    <groupId>org.apache.poi</groupId>

    <artifactId>poi-scratchpad</artifactId>

    <version>3.16</version>

  </dependency>

  <dependency>

    <groupId>fr.opensagres.xdocreport</groupId>

    <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>

    <version>2.0.2</version>

  </dependency>

  <dependency>

    <groupId>com.deepoove</groupId>

    <artifactId>poi-tl</artifactId>

    <version>1.0.0</version>

    <exclusions>

        <exclusion>

            <artifactId>org.slf4j</artifactId>

            <groupId>slf4j-api</groupId>

        </exclusion>

        <exclusion>

            <artifactId>org.slf4j</artifactId>

            <groupId>slf4j-log4j12</groupId>

        </exclusion>

        <exclusion>

            <artifactId>org.apache.commons</artifactId>

            <groupId>commons-lang3</groupId>

        </exclusion>

    </exclusions>

  </dependency>

  

  

  <dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpclient</artifactId>

    <version>4.5.2</version>

  </dependency>

  <dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpcore</artifactId>

    <version>4.4.4</version>

  </dependency>

  <dependency>

    <groupId>org.nlpcn</groupId>

    <artifactId>elasticsearch-sql</artifactId>

    <version>6.2.3.0</version>

  </dependency>

  <dependency>

    <groupId>cn.hutool</groupId>

    <artifactId>hutool-all</artifactId>

    <version>4.5.8</version>

  </dependency>

 

 

致此,可利用uedior修改模板内容,再调用模板生成文档,适用于模板频繁变更

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值