组件一导出到Excel文件

时间:2014-02-28 03:19:25

标签: c# excel componentone

我正在尝试使用C1 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();

请帮助=)

1 个答案:

答案 0 :(得分: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");