2014-02-28 64 views
1

我試圖導出使用C1 excel,並提示用戶保存文件的位置。 但是我嘗試了下面的代碼,但它似乎並沒有工作。組件一導出到Excel文件

Response.Clear(); 
    Response.ContentType = "application/vnd.ms-excel"; 
    Response.AppendHeader("Content-Disposition","attachment;filename=CategoryReport.xls"); 

    System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
    xbook.Save(ms, C1.C1Excel.FileFormat.Biff8); 
    ms.WriteTo(Response.OutputStream); 
    ms.Close(); 
    ms.Dispose(); 

    xbook.Dispose(); 

請幫助=)

回答

0

您可以使用HTTP處理程序(DownloadFile.ashx):在後面的代碼

public class DownloadFile : IHttpHandler 
{ 
public void ProcessRequest(HttpContext context) 
{ 
    // retrieve your xbook 
    System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
    xbook.Save(ms, C1.C1Excel.FileFormat.Biff8); 
    xbook.Dispose(); 
    ms.Position = 0; 
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; 
    response.ClearContent(); 
    response.Clear(); 
    response.ContentType = "application/vnd.ms-excel"; 
    response.AddHeader("Content-Disposition","attachment;filename=CategoryReport.xls"); 
    Response.BufferOutput = true;   
    Response.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length); 
    response.Flush();  
    response.End(); 
} 

public bool IsReusable 
{ 
    get 
    { 
     return false; 
    } 
} 

}

在出口事件:

Response.Redirect("YourPathToHttpHandler/DownloadFile.ashx");