Click Button to Export Excel Asp.net C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public void ExportExcel(string excelLocation)
{
    FileStream fs = null;
    try
    {
        fs = File.OpenRead(excelLocation);
        Byte[] fileBytes = new byte[fs.Length];
        fs.Read(fileBytes, 0, Convert.ToInt32(fs.Length));
        //Clear the response              
        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.End();
    }
    finally
    {
        if (fs != null)
        {
            fs.Close();
            fs.Dispose();
        }
    }
}

  

Click Button to Export Excel Asp.net C#,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。