组件一导出到Excel文件
我试图导出使用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");