jsp txt excle 文档下载 写法

首先引用网上的解决方法
有两种方法可以实现

第一种是用输出流

response.setContentType("text/plain");
response.setHeader("Content-disposition","attachment;filename=" + "result.txt");
String text = "some text";
OutputStream out = response.getOutputStream();
out.write(text.getBytes());              
out.flush();
out.close();


这种有个问题就是不能换行,输出的字符串不能按你预想的格式来做,而是全在一行里,如果强行加“\r”,用记事本打开会看到一个小方块,用UltraEdit等文本编辑器打开可以看到换行。

第二种是用out.println直接打印
<%
response.setContentType("text/plain;charset=GBK");
response.setHeader("Content-disposition","attachment; filename=table.txt");
out.println("hello");
 %>


这种需要注意jsp文件中不能出现<%@ page 。。。%>出现一次就是一行,虽然这行是空的。

如果需要引用到其他的类,最好把类的报名全部写全,在代码中直接new,比如java.sql.ResultSet rs=null;

由于直接用println输出,所以可以在你想要的地方换行,用记事本和UltraEdit等文本编辑器打开都是正常换行。

我们的实现方法采用第一个
<%@page import="jxl.write.WritableFont"%><%@page import="jxl.write.BorderLineStyle"%><%@page import="jxl.format.Border"%><%@page import="jxl.format.Alignment"%><%@page import="jxl.write.WritableCellFormat"%><%@page import="java.io.FileInputStream"%><%@page import="com.chinacreator.bean.DateResult"%><%@page import="java.util.Iterator"%><%@page import="java.util.List"%><%@page import="java.util.ArrayList"%><%@page language="java" contentType="text/html; charset=gbk"
	     pageEncoding="gbk"%><%@page import="com.frameworkset.common.poolman.Record"%><%@page import="com.chinacreator.dao.ErrorDataDao"%>
<%@page import="java.io.OutputStream"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	ErrorDataDao dao = new ErrorDataDao();
	String objId = request.getParameter("object_id");//资源id
	String obj_name = dao.getObjName(objId);
	String fn = new String(obj_name.getBytes(),"ISO-8859-1");
	if("".equals(obj_name) || obj_name == null){
		System.out.print("请选择模板");
	}
	
	String obj_type = dao.getObjType(objId);	
	if(obj_type==".txt"){
  	  Record[] rname = dao.getColumnName(objId);
  	  String rs = dao.getSeparator(objId);
  	  String re = dao.getEnclosure(objId);
  	  
	  
  	  response.setHeader("Content-disposition","attachment; filename="+fn+".txt");	
	  response.setContentType("text/plain");		
  	  
  	  if(rname != null && rname.length > 0){
  		  StringBuffer OutPrint = new StringBuffer();
  		  for(int i=0; i<rname.length; i++){  			  
  			  OutPrint.append(rname[i].getString("COLUMN_NAME"));
  			  OutPrint.append(rname[i]);
  			  if(i==rname.length-1) break;
  			  OutPrint.append(rs);
  			  
  		  }
  		  OutPrint.append(re);
		  OutputStream outp = response.getOutputStream();
		  outp.write(OutPrint.toString().getBytes());              
		  outp.flush();
		  outp.close();
  	  }
  	  
	}
	else if(obj_type==".xls"){
		response.setHeader("Content-disposition","attachment; filename="+fn+".xls");
		response.setContentType("application/msexcel");
%>

<html>
	<head>
		<style>
			.tableformat td{mso-number-format: "\@";}
		</style>
	</head>
	<body>
		<table border="1" style="width: 100%; height: 100%;" class='tableformat'>
			<%
				Record[] r = dao.getUploadModel(objId);
				if(r != null && r.length > 0){
					out.print("<tr>");
					for (int i=0; i<r.length; i++) {
						out.print("<td>");
						out.print(r[i].getString("COLUMN_NAME"));
						out.print("</td>");
					}
					out.print("</tr>");
					
					for (int i=0; i<50; i++) {
						out.print("<tr>");
						for(int j=0; j<r.length; j++){
							out.print("<td> ");
							out.print("</td>");
						}
						out.print("</tr>");
					}
				}else{
					out.print("<tr><th colspan='10'>"+obj_name+"</th></tr>");
					out.print("<tr><th colspan='10'>无数据</th></tr>");
				}
			%>
		</table>
	</body>
</html>
<%
      }
%>

这段代码中可能报错,会出现getOutputStream()错误。解决方法
out.clear();
out = pageContext.pushBody();

你可能感兴趣的:(jsp)