首页 新闻 会员 周边

一个下载文件的问题,各位帮忙看下。。。急急急

0
[待解决问题]

需求:将数据导入到Excel里,并下载。

问题所在:在IE下,会下载当前页面的源代码。

              火狐下正常

 

代码:

 1       /// <summary>
 2         /// 数据导出到Excel 
 3         /// </summary>
 4         /// <param name="table">数据表</param>
 5         /// <param name="context">上下文对象</param>
 6         /// <param name="fileName">文件名</param>
 7         public static void RenderToExcelBrowser(DataTable table, HttpContext context, string fileName)
 8         {
 9             MemoryStream ms = new MemoryStream();
10             using (table)
11             {
12                 IWorkbook workbook = new XSSFWorkbook();
13                 ISheet sheet = workbook.CreateSheet();
14                 IRow headerRow = sheet.CreateRow(0);              
15                 foreach (DataColumn column in table.Columns)
16                     headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
17 
18                 // handling value.
19                 int rowIndex = 1;
20                 foreach (DataRow row in table.Rows)
21                 {
22                     IRow dataRow = sheet.CreateRow(rowIndex);
23                     foreach (DataColumn column in table.Columns)
24                     {
25                         dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
26                     }
27                     rowIndex++;
28                 }
29                 workbook.Write(ms);
30                 ms.Flush();
31             }//.xlsx
32             fileName=fileName+"-"+ DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xlsx";  
33 
34             context.Response.ContentType = "application/octet-stream";
35             context.Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);
36             context.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString()); 
37         
38             //context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
39             //System.Web.HttpContext.Current.Response.AddHeader("Content-Length", ms.ToArray().Length.ToString());
40             context.Response.BinaryWrite(ms.ToArray());
41         }
以人会友的主页 以人会友 | 初学一级 | 园豆:8
提问于:2013-05-23 09:39
< >
分享
所有回答(2)
0

一个简单的通过JSP中把数据导入到Excel,并出现下载对话框

ExcelJSPHTML 

主要是通过文件流的方式,这个方法不是很好,如有更好的方法,还希望多多交流哦!


<%@   page   contentType="text/html;charset=gb2312"   %>    
<%@   page   import="java.lang.*"%>  
<%  
  try{                                    
      response.setContentType("Application/msexcel");  
      response.setHeader("Content-Disposition",   "attachment;filename=test.xls");                            
      StringBuffer   cont=new   StringBuffer("");  
%>  
  <HTML>  
  <HEAD>  
  <META   http-equiv="Content-Style-Type"   content="text/css">      
  </HEAD>  
  <BODY>  
  <br/><br/><br/>  
<%    
  cont.append("<table   width='100%'   border='1'>\r\n");  
  cont.append("<tr>\r\n");  
  cont.append("<td   height='19'>字段一</td>\r\n");  
  cont.append("<td   height='19'>字段二</td>\r\n");  
  cont.append("<td   height='19'>字段三</td>\r\n");  
  cont.append("<td   height='19'>字段四</td>\r\n");  
  cont.append("</tr>\r\n");  
  cont.append("<tr>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
  cont.append("</tr>\r\n");  
  cont.append("<tr>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
  cont.append("<td>"+"xxxxxxxxxxxxxxxxxxxxxxxx"   +"&nbsp;</td>\r\n");  
   
  cont.append("</table>\r\n");    
  response.getWriter().println(cont.toString());  
  response.getWriter().close();  
   
  }  
  catch   (Exception   e){  
  out.println(e.toString());  
  }  
%>  
  </BODY>  
  </HTML>  

 

参考 www.baidu30ok.com

dwqfgqw8 | 园豆:223 (菜鸟二级) | 2013-05-23 17:36
0

1.先将数据导出到服务器

2.根据得到的服务器文件路径进行下载

string filePath=this.downLoadURL.Value;
            filePath.Replace("\\","/");
            string fileName = filePath.Substring(filePath.LastIndexOf('/')+1);//客户端保存的文件名
            filePath = Server.MapPath(filePath);
            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();

以上方式测试通过。

ClrsDream | 园豆:386 (菜鸟二级) | 2013-05-30 09:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册