asp.net 通過 Handler 匯出資料至excel (讓使用者下載)

來源:互聯網
上載者:User

標籤:tty   term   new   dmi   for   header   分享   bsp   state   

:

 

代碼:

Export2Excel.ashx

1 <%@ WebHandler Language="C#" CodeBehind="Export2Excel.ashx.cs" Class="BLIC.SecurityCodeValidate.Web.Handler.Export2Excel" %>

Export2Excel.ashx.cs

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5 using System.Web.SessionState;  6 using System.IO;  7 using System.Data;  8   9 namespace BLIC.SecurityCodeValidate.Web.Handler 10 { 11     /// <summary> 12     /// AdminLogin 的摘要說明 13     /// </summary> 14     public class Export2Excel : IHttpHandler, IRequiresSessionState 15     { 16  17         public void ProcessRequest(HttpContext context) 18         { 19  20  21             try 22             { 23                 test1(context); 24             } 25             catch (Exception ex) 26             { 27             } 28  29             //try 30             //{ 31             //    test1(context); 32             //} 33             //catch (Exception ex) 34             //{ 35             //    //context.Response.ContentType = "text/plain"; 36             //    context.Response.Write("匯出失敗:" + ex.Message); 37             //} 38         } 39  40         public bool IsReusable 41         { 42             get 43             { 44                 return false; 45             } 46         } 47  48         private void test1(HttpContext context) 49         { 50             HttpResponse resp = System.Web.HttpContext.Current.Response; 51             resp.Charset = "utf-8"; 52             resp.Clear(); 53             string filename = "統計貼標報表_" + DateTime.Now.ToString("yyyyMMddHHmmss"); 54             resp.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); 55             resp.ContentEncoding = System.Text.Encoding.UTF8; 56  57             resp.ContentType = "application/ms-excel"; 58             string style = "<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=utf-8\"/>" + "<style> .table{ font: 9pt Tahoma, Verdana; color: #000000; text-align:center;  background-color:#8ECBE8;  }.table td{text-align:center;height:21px;background-color:#EFF6FF;}.table th{ font: 9pt Tahoma, Verdana; color: #000000; font-weight: bold; background-color: #8ECBEA; height:25px;  text-align:center; padding-left:10px;}</style>"; 59             resp.Write(style); 60   61             resp.Write("<table class=‘table‘><tr><th>姓名</th><th>出生年月</th><th>籍貫</th><th>畢業時間</th></tr>"); 62             63             System.Data.DataTable dtSource = new System.Data.DataTable(); 64             dtSource.TableName = "statistic"; 65             dtSource.Columns.Add("第一列"); 66             dtSource.Columns.Add("第二列"); 67             dtSource.Columns.Add("第三列"); 68             dtSource.Columns.Add("第四列"); 69  70             System.Data.DataRow row = null; 71             row = dtSource.NewRow(); 72             row[0] = "張三"; 73             row[1] = "1987-09-09"; 74             row[2] = "河北保定"; 75             row[3] = "2008年畢業"; 76             dtSource.Rows.Add(row); 77  78             row = dtSource.NewRow(); 79             row[0] = "李四"; 80             row[1] = "1987-09-02"; 81             row[2] = "湖北武漢"; 82             row[3] = "2009年畢業"; 83             dtSource.Rows.Add(row); 84  85             row = dtSource.NewRow(); 86             row[0] = "王五"; 87             row[1] = "1987-09-01"; 88             row[2] = "湖南湘潭"; 89             row[3] = "2013年畢業"; 90             dtSource.Rows.Add(row); 91  92             foreach (DataRow tmpRow in dtSource.Rows) 93             { 94                 resp.Write("<tr><td>" + tmpRow[0] + "</td>"); 95                 resp.Write("<td>" + tmpRow[1] + "</td>"); 96                 resp.Write("<td>" + tmpRow[2] + "</td>"); 97                 resp.Write("<td>" + tmpRow[3] + "</td>"); 98                 resp.Write("</tr>"); 99             }100             resp.Write("<table>");101 102             resp.Flush();103             resp.End();104         }105   106     }107 }

Js部分內容

1 $("#Excel").bind(‘click‘, function () {2                 var url = "AjaxHandler/RadioFamilyDayNumber.ashx?Action=Excel";3                 window.location.href = url;//匯出Excel4             })

 

asp.net 通過 Handler 匯出資料至excel (讓使用者下載)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.