ASP.NET中怎么把服务器上生成的EXCEL推送到客户端下载

时间:2021-02-15 17:53:16
我现在可以保存excel了,不到怎么把内存中生成的excel直接让客户端下载,而不保存在服务器
xApp.ActiveWorkbook.SaveAs("c:\\test.xls", Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null,null);

8 个解决方案

#1


lz,可以参考这个


Client Side Script to Export To Excel 2007


然后关注: fso.GetSpecialFolder(2) gets the complete folder path of TEMP folder

#2


protected virtual void ExportToExcel(DataGrid dataGrid)
{
 string sheetName = "Sheet";

 this.Response.Clear();
 this.Response.Charset = string.Empty; 
 this.Response.ContentType = "application/vnd.ms-excel";
 this.Response.AddHeader("Pragma", "public");
 this.Response.AddHeader("Cache-Control", "max-age=0");
 this.Response.AppendHeader("content-disposition", "attachment; filename=" + sheetName + ".xls");

 System.IO.StringWriter stringWriter = new System.IO.StringWriter(); 
 System.Web.UI.HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter); 

 this.EnableViewState = false;

 for (int i=0; i<dataGrid.Items.Count; i++)
 {
  for (int j=0; j<dataGrid.Items[i].Cells.Count; j++)
  {
   dataGrid.Items[i].Cells[j].Style.Add("mso-number-format", "\\@");
  }
 }
 dataGrid.RenderControl(htmlTextWriter);

 this.Response.Write(stringWriter.ToString());
 this.Response.End();
}

#3


生成流,

 HttpResponse response = HttpContext.Current.Response;

            response.Charset = "utf-8";
            response.ContentEncoding = Encoding.GetEncoding("utf-8");
            response.ContentType = "application/ms-excel/msword";
            response.AppendHeader("Content-Disposition", "attachment;filename=" +
                HttpUtility.UrlEncode(_fileName));

            response.Write(_xlstext.ToString());
            response.End();

#4


上面那几个好象都要先保存excel到硬盘上然后在输出,能不能直接在内存中输出到客户端,而不保存

#5


3楼的就可以不会保存到硬盘上

#6


string str="<table>"
for (int i = 0; i < oDS.Tables[0].Rows.Count; i++)
{
 str += "<tr>";
 str += "<td style='vnd.ms-excel.numberformat:@'>" + oDS.Tables[0].Rows[i]["FORCAST_MEMO"]
  .ToString().Trim() + "</td>";
 }
 str += "</table>";

  Response.Clear();
  Response.Buffer = true;
  Response.ContentType = "application/vnd.ms-excel";
  Response.AddHeader("content-disposition", "attachement; filename=Sheet.xls");
  Response.Charset = "UTF-7";
  Response.Write(str);
  Response.Flush();
  Response.Close();
                  

#7


把3楼的这句
this.Response.AppendHeader("content-disposition", "attachment; filename=" + sheetName + ".xls");
里面的attachment改成online

#8


保存到硬盘以后给用户一个超链接就可以了!
把路径给用户,直接就提示下载了.

#1


lz,可以参考这个


Client Side Script to Export To Excel 2007


然后关注: fso.GetSpecialFolder(2) gets the complete folder path of TEMP folder

#2


protected virtual void ExportToExcel(DataGrid dataGrid)
{
 string sheetName = "Sheet";

 this.Response.Clear();
 this.Response.Charset = string.Empty; 
 this.Response.ContentType = "application/vnd.ms-excel";
 this.Response.AddHeader("Pragma", "public");
 this.Response.AddHeader("Cache-Control", "max-age=0");
 this.Response.AppendHeader("content-disposition", "attachment; filename=" + sheetName + ".xls");

 System.IO.StringWriter stringWriter = new System.IO.StringWriter(); 
 System.Web.UI.HtmlTextWriter htmlTextWriter = new System.Web.UI.HtmlTextWriter(stringWriter); 

 this.EnableViewState = false;

 for (int i=0; i<dataGrid.Items.Count; i++)
 {
  for (int j=0; j<dataGrid.Items[i].Cells.Count; j++)
  {
   dataGrid.Items[i].Cells[j].Style.Add("mso-number-format", "\\@");
  }
 }
 dataGrid.RenderControl(htmlTextWriter);

 this.Response.Write(stringWriter.ToString());
 this.Response.End();
}

#3


生成流,

 HttpResponse response = HttpContext.Current.Response;

            response.Charset = "utf-8";
            response.ContentEncoding = Encoding.GetEncoding("utf-8");
            response.ContentType = "application/ms-excel/msword";
            response.AppendHeader("Content-Disposition", "attachment;filename=" +
                HttpUtility.UrlEncode(_fileName));

            response.Write(_xlstext.ToString());
            response.End();

#4


上面那几个好象都要先保存excel到硬盘上然后在输出,能不能直接在内存中输出到客户端,而不保存

#5


3楼的就可以不会保存到硬盘上

#6


string str="<table>"
for (int i = 0; i < oDS.Tables[0].Rows.Count; i++)
{
 str += "<tr>";
 str += "<td style='vnd.ms-excel.numberformat:@'>" + oDS.Tables[0].Rows[i]["FORCAST_MEMO"]
  .ToString().Trim() + "</td>";
 }
 str += "</table>";

  Response.Clear();
  Response.Buffer = true;
  Response.ContentType = "application/vnd.ms-excel";
  Response.AddHeader("content-disposition", "attachement; filename=Sheet.xls");
  Response.Charset = "UTF-7";
  Response.Write(str);
  Response.Flush();
  Response.Close();
                  

#7


把3楼的这句
this.Response.AppendHeader("content-disposition", "attachment; filename=" + sheetName + ".xls");
里面的attachment改成online

#8


保存到硬盘以后给用户一个超链接就可以了!
把路径给用户,直接就提示下载了.