利用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 buildReultMap(String body) {String[] ar = body.split("}}");List 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 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 = "" + stringBuffer.toString() + "";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.WordDocumentfs.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 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.songxinqianggroupId>
<artifactId>com.baidu.ueditorartifactId>
<version>1.1.2-officalversion>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-scratchpadartifactId>
<version>3.16version>
dependency>
<dependency>
<groupId>fr.opensagres.xdocreportgroupId>
<artifactId>fr.opensagres.xdocreport.converter.docx.xwpfartifactId>
<version>2.0.2version>
dependency>
<dependency>
<groupId>com.deepoovegroupId>
<artifactId>poi-tlartifactId>
<version>1.0.0version>
<exclusions>
<exclusion>
<artifactId>org.slf4jartifactId>
<groupId>slf4j-apigroupId>
exclusion>
<exclusion>
<artifactId>org.slf4jartifactId>
<groupId>slf4j-log4j12groupId>
exclusion>
<exclusion>
<artifactId>org.apache.commonsartifactId>
<groupId>commons-lang3groupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.5.2version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpcoreartifactId>
<version>4.4.4version>
dependency>
<dependency>
<groupId>org.nlpcngroupId>
<artifactId>elasticsearch-sqlartifactId>
<version>6.2.3.0version>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>4.5.8version>
dependency>

 

 

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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部