利用Uedior做模板生成word文档
新建模板时选择不同的模板类型出现不同的模板参数
不同的模板类型出现不同的模板参数代码如下
switch (type) {
|
自定义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语句,map为key(参数名),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修改模板内容,再调用模板生成文档,适用于模板频繁变更