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             })
  • 相关阅读:
    6.VUE事件处理
    springmvc在使用@ModelAttribute注解获取Request和Response会产生线程并发不安全问题
    IDEAhttp://lookdiv.com/index/index/indexcodeindex.html
    不四舍五入保留...4(round(273.86015,4,1);)
    spring security中@PreAuthorize、@PostAuthorize、@PreFilter和@PostFilter四者的区别
    @RepeatSubmit spring boot 防止重复提交
    权限设计的杂谈
    vue设置全局样式变量 less
    坐标轴刻度取值算法-基于魔数数组-源于echarts的y轴刻度计算需求
    less使用
  • 原文地址:https://www.cnblogs.com/Jeremy2001/p/6731238.html
Copyright © 2011-2022 走看看