使用Response.TransmitFile()导出到excel无法正常工作

时间:2016-03-10 05:23:10

标签: asp.net asp.net-mvc asp.net-mvc-3 openxml

我已经使用OpenXML向Excel文件写了一些内容,并尝试使用Reponse.TransmitFile传输文件,但它无效。当我调试代码时,它不会抛出任何异常。

string filename = "Book1.xlsx";

try {
    System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
    Response.ClearContent();
    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Length", Server.MapPath("~/" + filename).Length.ToString());
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename);
    Response.TransmitFile(Server.MapPath("~/"+filename));
    Response.Flush();
    Response.End();
}
catch (Exception e) {
    Console.WriteLine(e);
}

0 个答案:

没有答案