java导出数据到word(二)

一、首先设计一个静态的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>创建人:张三&nbsp;&nbsp;</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>

注:表格只要加边框就好,转换后自动会有内边框

171556_rRoK_1987856.png

二、把设计好的页面,改名为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");


转载于:https://my.oschina.net/u/1987856/blog/609356

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值