java导出数据到word(二)
2016-01-26 00:00
483 查看
摘要: 利用freemarker的模板以及标签,把数据渲染到模板上,获取模板HTML字符串,利用Poi把HTML字符串转化为word文档,
如果有图片需要jsoup对img标签进行特殊处理。
一、首先设计一个静态的HTML页面,根据需要导出word设计布局。
注:表格只要加边框就好,转换后自动会有内边框
二、把设计好的页面,改名为ftl后缀
三、后台代码
(1)把poi相关依赖包,放入项目中
(2)具体实现
四、页面
如果有图片需要jsoup对img标签进行特殊处理。
一、首先设计一个静态的HTML页面,根据需要导出word设计布局。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>互动建议列表</title> </head> <body> <h3 style="text-align: center;">互动标题</h3> <div> <div>互动内容</div> <p style="text-align: right;"><span>创建人:张三 </span><span>创建时间:2016-02-03</span></p> </div> <h5 style="text-align: center;">意见列表</h5> <table style="width: 100%;border: none;" cellspacing="0" cellpadding="0"> <tbody > <tr > <td style="width: 25%;text-align: right;border-top:none;border-left:none;border-bottom:none;border-right:none;">创建人:</td> <td style="width: 25%;border-top:none;border-left:none;border-bottom:none;border-right:none;">张三</td> <td style="width: 25%;text-align: right;border-top:none;border-left:none;border-bottom:none;border-right:none;">创建时间:</td> <td style="width: 25%;border-top:none;border-left:none;border-bottom:none;border-right:none;">2016-02-03</td> </tr> <tr> <td style="text-align: right;border-top:none;border-left:none;border-bottom:none;border-right:none;">意见内容:</td> <td colspan="3" style="border-top:none;border-left:none;border-bottom:none;border-right:none;" >1111</td> </tr> </tbody> </table> </body> </html>
注:表格只要加边框就好,转换后自动会有内边框
二、把设计好的页面,改名为ftl后缀
三、后台代码
(1)把poi相关依赖包,放入项目中
(2)具体实现
/** * * <p>【导出doc文档】</p> * <p>条件:</p> * <p>备注:</p> * <p>例子:</p> * <p>日志:</p> * * @author:zhu [2016年2月3日 下午5:11:04] */ public void outOfDocGeneral() { StringWriter stringOut = new StringWriter(); ServletOutputStream out = null; ByteArrayInputStream bais = null; try { //获取数据 hdJyList = hdJyService.findByCreater(hd.getId(), getCurrentUser().getId()); hd = hdService.load(hd.getId()); Configuration configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); //获取模板根路径 configuration.setClassForTemplateLoading(this.getClass(), "../../../../../docTemplate"); //设置对于填充数据 Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("title", hd.getHdbt()); dataMap.put("creater", hd.getLrr()); org.jsoup.nodes.Document doc = Jsoup.parse(hd.getHdnr()); Elements eles = doc.getElementsByTag("img"); String url = getUrl(); for (Element element : eles) { element.attr("src", url + element.attr("src")); } dataMap.put("content", doc.html()); dataMap.put("createTime", hd.getLrsj()); dataMap.put("hdJyList", hdJyList); //获取模板 Template temp = configuration.getTemplate("hdJyList.ftl"); //填充模板 temp.process(dataMap, stringOut); //获取整个HTML字符串 String content = stringOut.toString(); byte b[] = content.getBytes("UTF-8"); bais = new ByteArrayInputStream(b); POIFSFileSystem poifs = new POIFSFileSystem(); DirectoryEntry directory = poifs.getRoot(); DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); out = super.getWWResponse().getOutputStream();// 取得输出流 super.getWWResponse().reset();// 清空输出流 super.getWWResponse().setHeader("Content-disposition", "attachment; filename=proposal.doc");// 设定输出文件头 super.getWWResponse().setContentType("application/vnd.ms-word;charset=UTF-8");// 定义输出类型 poifs.writeFilesystem(out); bais.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (bais != null) { try { bais.close(); } catch (IOException e) { e.printStackTrace(); } } if (stringOut != null) { try { stringOut.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * <p>【获取当前请求url】</p> * <p>条件:</p> * <p>备注:</p> * <p>例子:</p> * <p>日志:</p> * * @return * @author:zhu [2016年2月3日 下午5:00:38] */ private String getUrl() { String url = ""; url = super.getWWRequest().getScheme() + "://" + super.getWWRequest().getServerName() + ":" + super.getWWRequest().getServerPort() + super.getWWRequest().getContextPath() + "/"; return url; }
四、页面
//后缀有个 .doc,页面就不会打开,自动下载关闭页面 window.open (url+"/hdAction!outOfDocGeneral.shtml?file=proposal.doc");
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树