zoukankan      html  css  js  c++  java
  • asp.net 通过 Handler 导出数据至excel (让用户下载)

    效果图:

    代码:

    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;//导出Excel
    4             })
  • 相关阅读:
    (16)JavaScript的流程控制(js的循环)
    (15)javaScript入门
    (14)定位布局(子级在父级中自由定位 父级在页面中自由定位)
    (0-1)CSS 标签语法的属性
    ACM/ICPC 之 双向链表_构造列表-模拟祖玛 (TSH OJ-Zuma(祖玛))
    手记-数学分析(高等数学)中有关算法效率的公式列举(O,Θ,Ω)
    2014北大研究生推免机试(校内)-复杂的整数划分(DP进阶)
    整数划分问题-解法汇总(暂有DP-递归)
    2014北大研究生推免机试(校内)-垃圾炸弹(基础枚举)
    ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
  • 原文地址:https://www.cnblogs.com/Jeremy2001/p/6731238.html
Copyright © 2011-2022 走看看