`
hbxflihua
  • 浏览: 661314 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java导出word

    博客分类:
  • java
 
阅读更多

实现导出功能,多数人都会选择一些热门的框架来完成。这样做无可厚非。但有些时候,只为一些简单的导出引入一个框架来说有点儿用牛刀杀鸡的感觉。java.io本事自带的导入导出功能就能够解决这些简单的问题。而且,导出框架也都是基于java.io进行的扩展。

下面简单介绍下将页面内容用java.io自带的功能实现导出。

 

要导出的页面内容如下:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base target="download"/>
<title>
	<s:property value="#request.title"/>
</title>
<jsp:include page="../common/meta.jsp"/>

<STYLE type="text/css">
	
</STYLE>
<script type="text/javascript">

	$(function(){
		$('#expinfo').val($('#s_info').html());
	});
	
	//打印2
	function printInfo(){
		//打印时将头尾两部分隐藏
		$('#header_div').css('display','none');
		window.print();	  
		$('#header_div').css('display','block');
	}
	
</script>
</head>

<body>


<div id="header_div">
	<form name="expform" id="expform" method="post" action="${root}/jxEval/rwexp.action">
		<table width="100%" cellpadding="0" cellspacing="0" border="0">
			<tr style="height:30px">
				<td width="6%">
					<textarea name="expinfo" id="expinfo" style="display: none;" ></textarea>
					<!-- 导出文件名,根据导出的文件名称后缀判断导出文件类型 -->
					<input type="hidden" name="fileName" id="fileName" value="<s:property value="#request.title"/>.doc">
				</td>
				<td align="right">
					<input type="submit" class="btn" value="导出" title="导出Word" style="width:60px"/>&nbsp;&nbsp;
					<input type="button" class="btn" value="打印" title="打印" style="width:60px" onclick="printInfo()"/>&nbsp;&nbsp;				
				</td>
			</tr>					
		</table>
	</form>
</div>

<div id="s_info">
//添加导出内容部分
</div>
<iframe id="download" name="download" height="0px" width="0px"></iframe>

</body>

</html>

 

这里,为了防止在导出时弹出一个页面,对页面做了如下处理:

 1、在head中添加base标签

<base target="download"/>

  2、在body中添加一个iframe

 

<iframe id="download" name="download" height="0px" width="0px"></iframe>

  

后台Action

private String fileName;//文件名
	
	public String getFileName()throws Exception {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	@Override
	public String execute() throws Exception {	
		
		String expinfo=request.getParameter("expinfo");
		if(!ValidateUtil.validateString(expinfo)){
			return null;
		}
	
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.reset();
		resp.setContentType("application/vnd.ms-word;charset=UTF-8");
		
		String strFileName = FileUtil.getFilePrefix(fileName);
		strFileName = URLEncoder.encode(strFileName, "UTF-8");
		String guessCharset = "gb2312";
		strFileName = new String(strFileName.getBytes(guessCharset), "ISO8859-1");

		resp.setHeader("Content-Disposition", "attachment;filename=" + strFileName + ".doc");
		
		OutputStream os = resp.getOutputStream();
	    os.write(expinfo.getBytes(), 0, expinfo.getBytes().length);
	    os.flush();
	    os.close();		
		return null;
		
	}

注意:

1、要对文件名进行编码,否则可能会出现无法导出。

2、在对outputstream执行write操作时,第三个参数请务必保持和第一个参数一致,否则可能会出现导出内容缺失的情况。

即写成

os.write(expinfo.getBytes(), 0, expinfo.getBytes().length);

而非

os.write(expinfo.getBytes(), 0, expinfo.length());

 原因很简单,你懂得。

 

对应struts.xml文件配置

<action name="rwexp" class="com.xxx.action.RwexpAction"></action> 

 由于action的方法中无需返回值,这里也就无需多做配置了。

 

 

 

分享到:
评论
4 楼 easy890819 2014-02-12  
我还以为是个大神,牛的一X呢,
仔细一看才知道是个渣渣,你这跟导出word有毛关系?创建一个空文档很爽么?
3 楼 ycyangcai 2013-09-09  
如果要导出带个网页怎么办?要好看的格式
2 楼 J2EE&forever 2013-07-09  
这导出word有毛关系,我擦
1 楼 madera 2013-02-05  
这和应该换个标题,叫导出文件...

相关推荐

Global site tag (gtag.js) - Google Analytics