繁体   English   中英

组件一导出到Excel文件

[英]Component One Exporting to Excel file

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

请帮忙=)

您可以使用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"); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM