菜鸟急急急,excel导出怎么获得一个保存路径,然后弹出打开保存对话框!望赐教 ssh都行我就是在做导入导出,导出的时候点击按钮跳转到Action方法,然后返回一个保存路径(不会指定这个要返回的路径问题1),然后弹出一会打开保存对话框,接收返回的路径,保存到本地冒个盘,(问题2打开保存对话框不会弄。)应该是这个流程吧。卡这很久了,望赐教各位有源码更好,有重谢。着急的很。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不要想复杂了。就2步,进Action 生成excel。然后下载。下载代码网上到处都有 LZ要清楚。保存路径是弹出保存对话框的时候自己选择的只能弹出保存文件名 /** * 下载服务器中的excel文件 * @param filePath * @param filename * @param response * @throws Exception */ public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{ // TODO Auto-generated method stub BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; InputStream fis = null; //File uploadFile = new File(filePath); File uploadFile = filePath; fis = new FileInputStream(uploadFile); bis = new BufferedInputStream(fis); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); filename = URLEncoder.encode(filename, "GBK"); //弹出下载对话框的关键代码,filename为显示的保存文件名称 response.setContentType("application/x-download"); response.setHeader("Content-Disposition","attachment;filename="+ filename); int bytesRead = 0; //都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流 byte[] buffer = new byte[8192]; while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) { try{ bos.write(buffer, 0, bytesRead); }catch(Exception e){} } try{bos.flush();}catch(Exception e){} try{bos.close();}catch(Exception e){} try{bis.close();}catch(Exception e){} try{fos.close();}catch(Exception e){} try{fis.close();}catch(Exception e){} } public void DownExcelFile(HttpServletResponse response,String fileDownPath) { File file = new File(fileDownPath); response.setContentType("text/plain;charset=utf-8"); if (file.exists()) { try { log.info("enter try"); // 要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成"application/x-msexcel"。 response.setContentType("application/x-msexcel"); // 保存文件名称 fileDownPath = fileDownPath.substring(fileDownPath.lastIndexOf("/") + 1); // 处理中文文件名// fileName = new String(fileName.getBytes("GB2312"), "utf-8"); //servlet中,要在 header中设置下载方式 response.setHeader("Content-Disposition","attachment; filename=" + fileDownPath); //FileInputStream输入流 //FileInputStream bis = new FileInputStream(file); //缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); //OutputStream输出流 OutputStream bos = response.getOutputStream(); byte[] buff = new byte[1024]; int readCount = 0; //每次从文件流中读1024个字节到缓冲里。 readCount = bis.read(buff); while (readCount != -1) { //把缓冲里的数据写入浏览器 bos.write(buff, 0, readCount); readCount = bis.read(buff); } if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } response.setStatus(HttpServletResponse.SC_OK); response.flushBuffer(); response.getWriter().close(); } catch (Exception e) { log.error("DownExcelFile error:" + e.getMessage()); } } } public void DownExcelFile(HttpServletResponse response,String fileDownPath) { File file = new File(fileDownPath); response.setContentType("text/plain;charset=utf-8"); if (file.exists()) { try { log.info("enter try"); // 要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成"application/x-msexcel"。 response.setContentType("application/x-msexcel"); // 保存文件名称 fileDownPath = fileDownPath.substring(fileDownPath.lastIndexOf("/") + 1); // 处理中文文件名// fileName = new String(fileName.getBytes("GB2312"), "utf-8"); //servlet中,要在 header中设置下载方式 response.setHeader("Content-Disposition","attachment; filename=" + fileDownPath); //FileInputStream输入流 //FileInputStream bis = new FileInputStream(file); //缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); //OutputStream输出流 OutputStream bos = response.getOutputStream(); byte[] buff = new byte[1024]; int readCount = 0; //每次从文件流中读1024个字节到缓冲里。 readCount = bis.read(buff); while (readCount != -1) { //把缓冲里的数据写入浏览器 bos.write(buff, 0, readCount); readCount = bis.read(buff); } if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } response.setStatus(HttpServletResponse.SC_OK); response.flushBuffer(); response.getWriter().close(); } catch (Exception e) { log.error("DownExcelFile error:" + e.getMessage()); } } } String str = request.getParameter("exstr"); //str = java.net.URLDecoder.decode(str, "UTF-8"); ServletOutputStream sos = response.getOutputStream();// 获取输出流 response.reset(); response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode("文件名称", "UTF8") + ".xls" + "\"");// 设定输出文件头 response.setContentType("application/msexcel");// 设定输出类型 OutputStreamWriter osw = new OutputStreamWriter(sos,"gbk");//设置编码以及内容 osw.write(str); osw.flush(); osw.close(); sos.close(); 我想问我导出是要返回文件名和文件的file是吗 //弹出下载对话框的关键代码,filename为显示的保存文件名称 response.setContentType("application/x-download"); response.setHeader("Content-Disposition","attachment;filename="+ filename);filename是返回的文件名,在另存为的时候也可以改的。。file已经被写入到一个输出流中。。另存为的时候就能取出。。 String filepath = "e:\\文件名.xls"; FileInputStream fis = new FileInputStream(new File(filepath)); OutputStream os = response.getOutputStream(); response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode("文件名.xls", "utf-8")); int i = 0; byte[] b = new byte[8192]; while ((i = fis.read(b, 0, 8192)) != -1) { os.write(b, 0, i); } os.flush(); fis.close(); return null; 帅哥你这个filePath是写导出方法的时候返回的创建workBook的是作为参数的new file(“d:/t.xls”);的引用名吗 我想问下3楼的filePath是写导出方法的时候返回的创建workBook的是作为参数的new file(“d:/t.xls”);的引用名吗 汗!!!我代码中只是将两者名称都用filename参数罢了你要用别的名字可以随便取 我说你的file类型的参数是传什么,你理解错了,我肯定知道随便改名字 file类型参数。。是服务器上面的文件。。如excel文件经过了File file = new File("...")包装过的。意思就是需要下载的文件。。代码中难道不是很明显就能看出来吗? j2ee高级部分的视频教程分享 spring 事务回滚问题 IE8如何在兼容模式下添加信任 如何实现不生成文件,但是可以直接下载,要求是PDF格式 请教spring+acegi 使用window.close后用户失效问题 哪位仁兄那有用java实现的ftp连接其中包括上传下载文件和文件列表, 及获得文件后缀和后缀列表 请帮个忙 小弟感激不尽 EJB分为哪几种?什么时候用本地接口?什么时候用远程接口? 请问weblogic的几个入门问题 接口 各位大侠,有没有好的STRUTS入门的资料给小弟分享一下啊 AXIS2 发送对象问题 关于Extjs的Ext.onReady的问题
* 下载服务器中的excel文件
* @param filePath
* @param filename
* @param response
* @throws Exception
*/
public static void download(File filePath, String filename, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null; //File uploadFile = new File(filePath);
File uploadFile = filePath;
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
filename = URLEncoder.encode(filename, "GBK");
//弹出下载对话框的关键代码,filename为显示的保存文件名称
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);
int bytesRead = 0;
//都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
try{
bos.write(buffer, 0, bytesRead);
}catch(Exception e){}
}
try{bos.flush();}catch(Exception e){}
try{bos.close();}catch(Exception e){}
try{bis.close();}catch(Exception e){}
try{fos.close();}catch(Exception e){}
try{fis.close();}catch(Exception e){}
}
File file = new File(fileDownPath);
response.setContentType("text/plain;charset=utf-8");
if (file.exists()) {
try {
log.info("enter try");
// 要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成"application/x-msexcel"。
response.setContentType("application/x-msexcel");
// 保存文件名称
fileDownPath = fileDownPath.substring(fileDownPath.lastIndexOf("/") + 1);
// 处理中文文件名
// fileName = new String(fileName.getBytes("GB2312"), "utf-8");
//servlet中,要在 header中设置下载方式
response.setHeader("Content-Disposition","attachment; filename=" + fileDownPath);
//FileInputStream输入流
//FileInputStream bis = new FileInputStream(file);
//缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
//OutputStream输出流
OutputStream bos = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
//每次从文件流中读1024个字节到缓冲里。
readCount = bis.read(buff);
while (readCount != -1) {
//把缓冲里的数据写入浏览器
bos.write(buff, 0, readCount);
readCount = bis.read(buff);
}
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
response.setStatus(HttpServletResponse.SC_OK);
response.flushBuffer();
response.getWriter().close();
} catch (Exception e) {
log.error("DownExcelFile error:" + e.getMessage());
}
}
}
public void DownExcelFile(HttpServletResponse response,String fileDownPath) {
File file = new File(fileDownPath);
response.setContentType("text/plain;charset=utf-8");
if (file.exists()) {
try {
log.info("enter try");
// 要用servlet 来打开一个 EXCEL 文档,需要将 response 对象中 header 的 contentType 设置成"application/x-msexcel"。
response.setContentType("application/x-msexcel");
// 保存文件名称
fileDownPath = fileDownPath.substring(fileDownPath.lastIndexOf("/") + 1);
// 处理中文文件名
// fileName = new String(fileName.getBytes("GB2312"), "utf-8");
//servlet中,要在 header中设置下载方式
response.setHeader("Content-Disposition","attachment; filename=" + fileDownPath);
//FileInputStream输入流
//FileInputStream bis = new FileInputStream(file);
//缓冲流(BufferedStream)可以一次读写一批数据,,缓冲流(Buffered Stream)大大提高了I/O的性能。
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
//OutputStream输出流
OutputStream bos = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
//每次从文件流中读1024个字节到缓冲里。
readCount = bis.read(buff);
while (readCount != -1) {
//把缓冲里的数据写入浏览器
bos.write(buff, 0, readCount);
readCount = bis.read(buff);
}
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
response.setStatus(HttpServletResponse.SC_OK);
response.flushBuffer();
response.getWriter().close();
} catch (Exception e) {
log.error("DownExcelFile error:" + e.getMessage());
}
}
}
//str = java.net.URLDecoder.decode(str, "UTF-8");
ServletOutputStream sos = response.getOutputStream();// 获取输出流
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\""
+ URLEncoder.encode("文件名称", "UTF8") + ".xls"
+ "\"");// 设定输出文件头
response.setContentType("application/msexcel");// 设定输出类型
OutputStreamWriter osw = new OutputStreamWriter(sos,"gbk");//设置编码以及内容
osw.write(str);
osw.flush();
osw.close();
sos.close();
我想问我导出是要返回文件名和文件的file是吗
//弹出下载对话框的关键代码,filename为显示的保存文件名称
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+ filename);filename是返回的文件名,在另存为的时候也可以改的。。
file已经被写入到一个输出流中。。另存为的时候就能取出。。
String filepath = "e:\\文件名.xls";
FileInputStream fis = new FileInputStream(new File(filepath));
OutputStream os = response.getOutputStream(); response.setHeader("Content-disposition","attachment;filename=" +
URLEncoder.encode("文件名.xls", "utf-8"));
int i = 0;
byte[] b = new byte[8192];
while ((i = fis.read(b, 0, 8192)) != -1)
{
os.write(b, 0, i);
}
os.flush();
fis.close();
return null;
帅哥你这个filePath是写导出方法的时候返回的创建workBook的是作为参数的new file(“d:/t.xls”);的引用名吗
我想问下3楼的filePath是写导出方法的时候返回的创建workBook的是作为参数的new file(“d:/t.xls”);的引用名吗
汗!!!我代码中只是将两者名称都用filename参数罢了你要用别的名字可以随便取
意思就是需要下载的文件。。代码中难道不是很明显就能看出来吗?