如何让用户下载文档哦 1.要求是我产生xls的内容,(这我完成了)2.让后把xls内容返回给浏览器。(听说这样的话浏览器就会自动提供用户下栽)我没做这个,云里雾里的 请大家帮帮忙啊,给点提示也行哈。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.newasp.net/tech/java/15526.html 提供你文件的地址就可以了,放到<a>里 response.getOutputStream();到前台 哈哈哈.我查了一下,可以使用JasperGrails. 把response的content-type改一下就可以了一般的都是text/html之类的,如果该成别的浏览器会自动提示用户下载的 excel 的content-typeapplication/vnd.ms-excel <%@ page language="java" contentType="text/xml; charset=GBK" pageEncoding="GBK"%>这样就不用你生成EXCEL,数据直接显示就可以了。 下载分两种,HTTP和文件流下载。一般浏览器自带下载功能,你只要提供下载地址就行了。例如function downloan(url) { window.location = url;}把你下载的地址传页面上,写上这么一句就可以实现了。对于I/O流和HTTP怎么下载,百度里面很多。如果还有什么问题,可以加我QQ进一步讨论。 为什么文挡下下来的时候不是xls类型的呢?response.contentType = "application/vnd.ms-excel" 研究研究html,你会发现很神奇哦。说白了就是浏览器以哪种格式呈现给用户。 response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); 把名字写在filename中response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); 默认情况下提供的url会用浏览器打开,这个可以在web.xml中来配置mime类型来决定哪些类型提示下载 我用过哦 这个就是我用来导出的 但是他还是让我以servlet的名字为名字 下栽下来的档也不是xls类型的 可让我郁闷了恩久恩久啊。。大家还有什么好的建议啊 难道 setHeader 错了 请高人指教啊ByteArrayOutputStream bin = getExcelService.getResponse(session, params) byte[] bytes = bin.toByteArray() response.contentType = "application/vnd.ms-excel" response.outputStream << bytes response.setHeader("Content-disposition","attachment;filename = winner.xls" ); 以下是我原先一个项目上的:1.是先查询到数据;2.将数据使用poi生成xls文件;3.设定响应类型及响应头信息(含文件名及文件扩展名.xls)4.是将xls工作薄写入输出流;//根据页码和最多显示行数查询出数据 List lst=exceptsvs.selectExcept(condition, pageNum, maxResults); //获得今天的日期 SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd"); String today=simpledate.format(new Date()); //实例一个工作薄对象 HSSFWorkbook wb = new HSSFWorkbook(); //在工作薄中创建一张工作表 HSSFSheet sheet = wb.createSheet("异常案例表("+today+")"); //声明一行 HSSFRow row = null; //声明一个单元格 HSSFCell cell = null; //建立第一行 row=sheet.createRow((short)0); cell=row.createCell((short)0);//建立一个单元格 cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设定单元格内容的编码为双位 cell.setCellValue("产品类别"); cell=row.createCell((short)1);//建立一个单元格 cell.setCellValue("机种名称"); cell=row.createCell((short)2);//建立一个单元格 cell.setCellValue("主件料号"); cell=row.createCell((short)3);//建立一个单元格 cell.setCellValue("厂别"); cell=row.createCell((short)4);//建立一个单元格 cell.setCellValue("阶段"); cell=row.createCell((short)5);//建立一个单元格 cell.setCellValue("线别"); cell=row.createCell((short)6);//建立一个单元格 cell.setCellValue("站别"); cell=row.createCell((short)7);//建立一个单元格 cell.setCellValue("班别"); cell=row.createCell((short)8);//建立一个单元格 cell.setCellValue("发生日期"); cell=row.createCell((short)9);//建立一个单元格 cell.setCellValue("停线时间"); cell=row.createCell((short)10);//建立一个单元格 cell.setCellValue("通知PE时间"); cell=row.createCell((short)11);//建立一个单元格 cell.setCellValue("PE处理结束时间"); cell=row.createCell((short)12);//建立一个单元格 cell.setCellValue("不良现象"); cell=row.createCell((short)13);//建立一个单元格 cell.setCellValue("原因分析"); cell=row.createCell((short)14);//建立一个单元格 cell.setCellValue("责任单位"); cell=row.createCell((short)15);//建立一个单元格 cell.setCellValue("处理PE"); cell=row.createCell((short)16);//建立一个单元格 cell.setCellValue("备注"); //从第二行开始循环 short r=1; for (Iterator iterator = lst.iterator(); iterator.hasNext();r++) { ExceptsView excepts = (ExceptsView) iterator.next();//取出一行数据 row=sheet.createRow(r); //建立一行 cell=row.createCell((short)0);//建立一个单元格 cell.setCellValue(excepts.getId().getProduct()); cell=row.createCell((short)1);//建立一个单元格 cell.setCellValue(excepts.getId().getModel()); cell=row.createCell((short)2);//建立一个单元格 cell.setCellValue(excepts.getId().getPartNo()); cell=row.createCell((short)3);//建立一个单元格 if(excepts.getId().getPlant()==0) {cell.setCellValue("一厂");} else {cell.setCellValue("六厂");} cell=row.createCell((short)4);//建立一个单元格 if(excepts.getId().getStep()==0) {cell.setCellValue("PVT前");} else if(excepts.getId().getStep()==1) {cell.setCellValue("PVT中");} else {cell.setCellValue("MP");} cell=row.createCell((short)5);//建立一个单元格 cell.setCellValue(excepts.getId().getLine()); cell=row.createCell((short)6);//建立一个单元格 cell.setCellValue(excepts.getId().getStation()); cell=row.createCell((short)7);//建立一个单元格 if(excepts.getId().getSquad()==0) {cell.setCellValue("白班");} else {cell.setCellValue("夜勤");} cell=row.createCell((short)8);//建立一个单元格 cell.setCellValue(excepts.getId().getExDate()); cell=row.createCell((short)9);//建立一个单元格 if(excepts.getId().getStopTime().trim().equals("")) {cell.setCellValue("未停线");} else {cell.setCellValue(excepts.getId().getStopTime());} cell=row.createCell((short)10);//建立一个单元格 cell.setCellValue(excepts.getId().getContactTime()); cell=row.createCell((short)11);//建立一个单元格 cell.setCellValue(excepts.getId().getFinishTime()); cell=row.createCell((short)12);//建立一个单元格 cell.setCellValue(excepts.getId().getSymptom()); cell=row.createCell((short)13);//建立一个单元格 cell.setCellValue(excepts.getId().getRootCase()); cell=row.createCell((short)14);//建立一个单元格 cell.setCellValue(excepts.getId().getDutyBu()); cell=row.createCell((short)15);//建立一个单元格 cell.setCellValue(excepts.getId().getName()); cell=row.createCell((short)16);//建立一个单元格 cell.setCellValue(excepts.getId().getRe()); } //文件名 String fname = "Excepts_table("+today+").xls"; //设定响应内容类型 response.setContentType("application/octet-stream"); //设定响应头信息,指定预设文件名 response.setHeader("Content-Disposition","attachment;fileName="+fname); //获得响应输出流对象 OutputStream outs=response.getOutputStream(); //将工作薄写入输出流 wb.write(outs); //关闭输出流对象 outs.close(); response.setHeader参数、用法的介绍 response.setHeader 是用来设置返回页面的头 meta 信息, 使用时 response.setHeader( name, contect ); meta是用来在HTML文档中模拟HTTP协议的响应头报文。meta 标签用于网页的<head>与</head>中 1、<meta name="Generator" contect="">用以说明生成工具(如Microsoft FrontPage 4.0)等;2、<meta name="KEYWords" contect="">向搜索引擎说明你的网页的关键词; 3、<meta name="DEscription" contect="">告诉搜索引擎你的站点的主要内容; 4、<meta name="Author" contect="你的姓名">告诉搜索引擎你的站点的制作的作者; 5、<meta name="Robots" contect= "all|none|index|noindex|follow|nofollow"> 其中的属性说明如下: 设定为all:文件将被检索,且页面上的链接可以被查询; 设定为none:文件将不被检索,且页面上的链接不可以被查询; 设定为index:文件将被检索; 设定为follow:页面上的链接可以被查询; 设定为noindex:文件将不被检索,但页面上的链接可以被查询; 设定为nofollow:文件将不被检索,页面上的链接可以被查询。 http-equiv属性 1、<meta http-equiv="Content-Type" contect="text/html";charset=gb_2312-80"> 和 <meta http-equiv="Content-Language" contect="zh-CN">用以说明主页制作所使用的文字以及语言;如英文是ISO-8859-1字符集,还有BIG5、utf-8、shift-Jis、Euc、Koi8-2等字符集;2、<meta http-equiv="Refresh" contect="n;url=http://yourlink/">定时让网页在指定的时间n内,跳转到页面http://yourlink/;3、<meta http-equiv="Expires" contect="Mon,12 May 2001 00:20:00 GMT">可以用于设定网页的到期时间,一旦过期则必须到服务器上重新调用。需要注意的是必须使用GMT时间格式; 4、<meta http-equiv="Pragma" contect="no-cache">是用于设定禁止浏览器从本地机的缓存中调阅页面内容,设定后一旦离开网页就无法从Cache中再调出; 5、<meta http-equiv="set-cookie" contect="Mon,12 May 2001 00:20:00 GMT">cookie设定,如果网页过期,存盘的cookie将被删除。需要注意的也是必须使用GMT时间格式; 6、<meta http-equiv="Pics-label" contect="">网页等级评定,在IE的internet选项中有一项内容设置,可以防止浏览一些受限制的网站,而网站的限制级别就是通过meta属性来设置的; 7、<meta http-equiv="windows-Target" contect="_top">强制页面在当前窗口中以独立页面显示,可以防止自己的网页被别人当作一个frame页调用; 8、<meta http-equiv="Page-Enter" contect="revealTrans(duration=10,transtion= 50)">和<meta http-equiv="Page-Exit" contect="revealTrans(duration=20,transtion=6)">设定进入和离开页面时的特殊效果,这个功能即FrontPage中的“格式/网页过渡”,不过所加的页面不能够是一个frame页面Cache-Control头域 Cache-Control指定请求和响应遵循的缓存机制。在请求消息或响应消息中设置Cache-Control并不会修改另一个消息处理过程中的缓存处理过程。请求时的缓存指令包括no-cache、no-store、max-age、max-stale、min-fresh、only-if-cached,响应消息中的指令包括public、private、no-cache、no-store、no-transform、must-revalidate、proxy-revalidate、max-age。各个消息中的指令含义如下: Public指示响应可被任何缓存区缓存。 Private指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效。 no-cache指示请求或响应消息不能缓存 no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。 max-age指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。 min-fresh指示客户机可以接收响应时间小于当前时间加上指定时间的响应。 max-stale指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以接收超出超时期指定值之内的响应消息。 response.setContentType()的String参数及对应类型 response.setContentType()的String参数及对应类型 <option value="image/bmp">BMP</option> <option value="image/gif">GIF</option> <option value="image/jpeg">JPEG</option> <option value="image/tiff">TIFF</option> <option value="image/x-dcx">DCX</option> <option value="image/x-pcx">PCX</option> <option value="text/html">HTML</option> <option value="text/plain">TXT</option> <option value="text/xml">XML</option> <option value="application/afp">AFP</option> <option value="application/pdf">PDF</option> <option value="application/rtf">RTF</option> <option value="application/msword">MSWORD</option> <option value="application/vnd.ms-excel">MSEXCEL</option> <option value="application/vnd.ms-powerpoint">MSPOWERPOINT</option> <option value="application/wordperfect5.1">WORDPERFECT</option> <option value="application/vnd.lotus-wordpro">WORDPRO</option> <option value="application/vnd.visio">VISIO</option> <option value="application/vnd.framemaker">FRAMEMAKER</option> <option value="application/vnd.lotus-1-2-3">LOTUS123</option> jaxb将xml绑定到java类 oscache.properties找不到 List的用法, 不解??? 如何让Apache + Tomcat 运行servlet 请问在jsp中分页应该怎么做 请问一下这两组函数有什么不同? 各位高手:如何实现站点间的数据传输 请教网页视频播放问题 aaa.jsp?id=电脑 这用中文好像会乱玛,怎么处理呢 我下载了websphere5,安装后怎么运行defaulthost?我的jsp放在那? myclipse6.5中怎样安装jbpm4.2???????? MySQL连接的url问题(用Java程序连接mysql时,在url中必须指定数据库名吗?)
到前台
一般的都是text/html之类的,如果该成别的
浏览器会自动提示用户下载的
application/vnd.ms-excel
这样就不用你生成EXCEL,数据直接显示就可以了。
一般浏览器自带下载功能,你只要提供下载地址就行了。
例如function downloan(url) {
window.location = url;
}
把你下载的地址传页面上,写上这么一句就可以实现了。
对于I/O流和HTTP怎么下载,百度里面很多。如果还有什么问题,可以加我QQ进一步讨论。
response.setHeader("Content-Disposition",
"attachment; filename=\"" + filename + "\"");
把名字写在filename中response.setHeader("Content-Disposition",
"attachment; filename=\"" + filename + "\"");
byte[] bytes = bin.toByteArray()
response.contentType = "application/vnd.ms-excel"
response.outputStream << bytes
response.setHeader("Content-disposition","attachment;filename = winner.xls" );
1.是先查询到数据;
2.将数据使用poi生成xls文件;
3.设定响应类型及响应头信息(含文件名及文件扩展名.xls)
4.是将xls工作薄写入输出流;//根据页码和最多显示行数查询出数据
List lst=exceptsvs.selectExcept(condition, pageNum, maxResults);
//获得今天的日期
SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
String today=simpledate.format(new Date());
//实例一个工作薄对象
HSSFWorkbook wb = new HSSFWorkbook();
//在工作薄中创建一张工作表
HSSFSheet sheet = wb.createSheet("异常案例表("+today+")");
//声明一行
HSSFRow row = null;
//声明一个单元格
HSSFCell cell = null;
//建立第一行
row=sheet.createRow((short)0);
cell=row.createCell((short)0);//建立一个单元格
cell.setEncoding(HSSFCell.ENCODING_UTF_16);//设定单元格内容的编码为双位
cell.setCellValue("产品类别");
cell=row.createCell((short)1);//建立一个单元格
cell.setCellValue("机种名称");
cell=row.createCell((short)2);//建立一个单元格
cell.setCellValue("主件料号");
cell=row.createCell((short)3);//建立一个单元格
cell.setCellValue("厂别");
cell=row.createCell((short)4);//建立一个单元格
cell.setCellValue("阶段");
cell=row.createCell((short)5);//建立一个单元格
cell.setCellValue("线别");
cell=row.createCell((short)6);//建立一个单元格
cell.setCellValue("站别");
cell=row.createCell((short)7);//建立一个单元格
cell.setCellValue("班别");
cell=row.createCell((short)8);//建立一个单元格
cell.setCellValue("发生日期");
cell=row.createCell((short)9);//建立一个单元格
cell.setCellValue("停线时间");
cell=row.createCell((short)10);//建立一个单元格
cell.setCellValue("通知PE时间");
cell=row.createCell((short)11);//建立一个单元格
cell.setCellValue("PE处理结束时间");
cell=row.createCell((short)12);//建立一个单元格
cell.setCellValue("不良现象");
cell=row.createCell((short)13);//建立一个单元格
cell.setCellValue("原因分析");
cell=row.createCell((short)14);//建立一个单元格
cell.setCellValue("责任单位");
cell=row.createCell((short)15);//建立一个单元格
cell.setCellValue("处理PE");
cell=row.createCell((short)16);//建立一个单元格
cell.setCellValue("备注"); //从第二行开始循环
short r=1;
for (Iterator iterator = lst.iterator(); iterator.hasNext();r++)
{
ExceptsView excepts = (ExceptsView) iterator.next();//取出一行数据
row=sheet.createRow(r); //建立一行
cell=row.createCell((short)0);//建立一个单元格
cell.setCellValue(excepts.getId().getProduct());
cell=row.createCell((short)1);//建立一个单元格
cell.setCellValue(excepts.getId().getModel());
cell=row.createCell((short)2);//建立一个单元格
cell.setCellValue(excepts.getId().getPartNo());
cell=row.createCell((short)3);//建立一个单元格
if(excepts.getId().getPlant()==0)
{cell.setCellValue("一厂");}
else
{cell.setCellValue("六厂");}
cell=row.createCell((short)4);//建立一个单元格
if(excepts.getId().getStep()==0)
{cell.setCellValue("PVT前");}
else if(excepts.getId().getStep()==1)
{cell.setCellValue("PVT中");}
else
{cell.setCellValue("MP");}
cell=row.createCell((short)5);//建立一个单元格
cell.setCellValue(excepts.getId().getLine());
cell=row.createCell((short)6);//建立一个单元格
cell.setCellValue(excepts.getId().getStation());
cell=row.createCell((short)7);//建立一个单元格
if(excepts.getId().getSquad()==0)
{cell.setCellValue("白班");}
else
{cell.setCellValue("夜勤");}
cell=row.createCell((short)8);//建立一个单元格
cell.setCellValue(excepts.getId().getExDate());
cell=row.createCell((short)9);//建立一个单元格
if(excepts.getId().getStopTime().trim().equals(""))
{cell.setCellValue("未停线");}
else
{cell.setCellValue(excepts.getId().getStopTime());}
cell=row.createCell((short)10);//建立一个单元格
cell.setCellValue(excepts.getId().getContactTime());
cell=row.createCell((short)11);//建立一个单元格
cell.setCellValue(excepts.getId().getFinishTime());
cell=row.createCell((short)12);//建立一个单元格
cell.setCellValue(excepts.getId().getSymptom());
cell=row.createCell((short)13);//建立一个单元格
cell.setCellValue(excepts.getId().getRootCase());
cell=row.createCell((short)14);//建立一个单元格
cell.setCellValue(excepts.getId().getDutyBu());
cell=row.createCell((short)15);//建立一个单元格
cell.setCellValue(excepts.getId().getName());
cell=row.createCell((short)16);//建立一个单元格
cell.setCellValue(excepts.getId().getRe());
}
//文件名
String fname = "Excepts_table("+today+").xls";
//设定响应内容类型
response.setContentType("application/octet-stream");
//设定响应头信息,指定预设文件名
response.setHeader("Content-Disposition","attachment;fileName="+fname);
//获得响应输出流对象
OutputStream outs=response.getOutputStream();
//将工作薄写入输出流
wb.write(outs);
//关闭输出流对象
outs.close();
response.setHeader 是用来设置返回页面的头 meta 信息,
使用时 response.setHeader( name, contect ); meta是用来在HTML文档中模拟HTTP协议的响应头报文。meta 标签用于网页的<head>与</head>中 1、<meta name="Generator" contect="">用以说明生成工具(如Microsoft FrontPage 4.0)等;2、<meta name="KEYWords" contect="">向搜索引擎说明你的网页的关键词; 3、<meta name="DEscription" contect="">告诉搜索引擎你的站点的主要内容; 4、<meta name="Author" contect="你的姓名">告诉搜索引擎你的站点的制作的作者; 5、<meta name="Robots" contect= "all|none|index|noindex|follow|nofollow">
其中的属性说明如下:
设定为all:文件将被检索,且页面上的链接可以被查询;
设定为none:文件将不被检索,且页面上的链接不可以被查询;
设定为index:文件将被检索;
设定为follow:页面上的链接可以被查询;
设定为noindex:文件将不被检索,但页面上的链接可以被查询;
设定为nofollow:文件将不被检索,页面上的链接可以被查询。 http-equiv属性
1、<meta http-equiv="Content-Type" contect="text/html";charset=gb_2312-80"> 和 <meta http-equiv="Content-Language" contect="zh-CN">用以说明主页制作所使用的文字以及语言;
如英文是ISO-8859-1字符集,还有BIG5、utf-8、shift-Jis、Euc、Koi8-2等字符集;2、<meta http-equiv="Refresh" contect="n;url=http://yourlink/">定时让网页在指定的时间n内,跳转到页面http://yourlink/;3、<meta http-equiv="Expires" contect="Mon,12 May 2001 00:20:00 GMT">可以用于设定网页的到期时间,一旦过期则必须到服务器上重新调用。需要注意的是必须使用GMT时间格式; 4、<meta http-equiv="Pragma" contect="no-cache">是用于设定禁止浏览器从本地机的缓存中调阅页面内容,设定后一旦离开网页就无法从Cache中再调出; 5、<meta http-equiv="set-cookie" contect="Mon,12 May 2001 00:20:00 GMT">cookie设定,如果网页过期,存盘的cookie将被删除。需要注意的也是必须使用GMT时间格式; 6、<meta http-equiv="Pics-label" contect="">网页等级评定,在IE的internet选项中有一项内容设置,可以防止浏览一些受限制的网站,而网站的限制级别就是通过meta属性来设置的; 7、<meta http-equiv="windows-Target" contect="_top">强制页面在当前窗口中以独立页面显示,可以防止自己的网页被别人当作一个frame页调用; 8、<meta http-equiv="Page-Enter" contect="revealTrans(duration=10,transtion= 50)">和<meta http-equiv="Page-Exit" contect="revealTrans(duration=20,transtion=6)">设定进入和离开页面时的特殊效果,这个功能即FrontPage中的“格式/网页过渡”,不过所加的页面不能够是一个frame页面Cache-Control头域
Cache-Control指定请求和响应遵循的缓存机制。在请求消息或响应消息中设置Cache-Control并不会修改另一个消息处理过程中的缓存处理过程。请求时的缓存指令包括no-cache、no-store、max-age、max-stale、min-fresh、only-if-cached,响应消息中的指令包括public、private、no-cache、no-store、no-transform、must-revalidate、proxy-revalidate、max-age。各个消息中的指令含义如下:
Public指示响应可被任何缓存区缓存。
Private指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效。
no-cache指示请求或响应消息不能缓存
no-store用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。
max-age指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。
min-fresh指示客户机可以接收响应时间小于当前时间加上指定时间的响应。
max-stale指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以接收超出超时期指定值之内的响应消息。
response.setContentType()的String参数及对应类型
<option value="image/bmp">BMP</option>
<option value="image/gif">GIF</option>
<option value="image/jpeg">JPEG</option>
<option value="image/tiff">TIFF</option>
<option value="image/x-dcx">DCX</option>
<option value="image/x-pcx">PCX</option>
<option value="text/html">HTML</option>
<option value="text/plain">TXT</option>
<option value="text/xml">XML</option>
<option value="application/afp">AFP</option>
<option value="application/pdf">PDF</option>
<option value="application/rtf">RTF</option>
<option value="application/msword">MSWORD</option>
<option value="application/vnd.ms-excel">MSEXCEL</option>
<option value="application/vnd.ms-powerpoint">MSPOWERPOINT</option>
<option value="application/wordperfect5.1">WORDPERFECT</option>
<option value="application/vnd.lotus-wordpro">WORDPRO</option>
<option value="application/vnd.visio">VISIO</option>
<option value="application/vnd.framemaker">FRAMEMAKER</option>
<option value="application/vnd.lotus-1-2-3">LOTUS123</option>