Click Button to Export Excel Asp.net C#

    public void ExportExcel(string excelLocation)
    {
        try
        {
            Byte[] fileBytes = File.ReadAllBytes(excelLocation);
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Cookies.Clear();
            //Add the header & other information      
            Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
            Response.CacheControl = "private";
            Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
            Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
            Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
            Response.AppendHeader("Pragma", "cache");
            Response.AppendHeader("Expires", "60");
            Response.AppendHeader("Content-Disposition",
            "attachment; " +
            "filename=\"ExcelReport.xlsx\"; " +
            "size=" + fileBytes.Length.ToString() + "; " +
            "creation-date=" + DateTime.Now.ToString("R") + "; " +
            "modification-date=" + DateTime.Now.ToString("R") + "; " +
            "read-date=" + DateTime.Now.ToString("R"));
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //Write it back to the client    
            Response.BinaryWrite(fileBytes);
            Response.Flush();
            Response.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

  

posted @ 2014-04-03 10:37  shzy2012  阅读(218)  评论(0编辑  收藏  举报