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#,布布扣,bubuko.com

Click Button to Export Excel Asp.net C#

上一篇:ORACLE如何设置多路复用control files


下一篇:MySQL 5.6初始配置调优