1

I'm trying export out using C1 excel with a prompt to the user where to save the file. However I have tried the following codes but it doesn't seems to work.

    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();

Please help =)

1 Answers1

0

You can use an HTTP Handler (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;
    }
}

}

In the export event in code behind:

 Response.Redirect("YourPathToHttpHandler/DownloadFile.ashx"); 
Mihai Hantea
  • 1,741
  • 13
  • 16