ASP. NET exports data to Excel via Handler (let users download)

Source: Internet
Author: User
Tags httpcontext

Code:

Export2excel.ashx

1 <%@ webhandler language="C #" codebehind="  Export2Excel.ashx.cs" class="BLIC. SecurityCodeValidate.Web.Handler.Export2Excel" %>

Export2Excel.ashx.cs

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.SessionState;6 usingSystem.IO;7 usingSystem.Data;8 9 namespaceBLIC. SecurityCodeValidate.Web.HandlerTen { One     /// <summary> A     ///Summary description of Adminlogin -     /// </summary> -      Public classExport2excel:ihttphandler, IRequiresSessionState the     { -  -          Public voidProcessRequest (HttpContext context) -         { +  -  +             Try A             { at test1 (context); -             } -             Catch(Exception ex) -             { -             } -  in             //Try -             //{ to             //test1 (context); +             //} -             //catch (Exception ex) the             //{ *             //    //context. Response.ContentType = "Text/plain"; $             //context. Response.Write ("Export failed:" + ex.) Message);Panax Notoginseng             //} -         } the  +          Public BOOLisreusable A         { the             Get +             { -                 return false; $             } $         } -  -         Private voidTest1 (HttpContext context) the         { -HttpResponse resp =System.Web.HttpContext.Current.Response;WuyiResp. Charset ="Utf-8"; the resp. Clear (); -             stringfilename ="Statistical Labeling Report _"+ DateTime.Now.ToString ("YYYYMMDDHHMMSS"); WuResp. Appendheader ("content-disposition","attachment;filename="+ filename +". xls"); -Resp. ContentEncoding =System.Text.Encoding.UTF8; About  $Resp. ContentType ="Application/ms-excel"; -             stringstyle ="<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>"; - resp. Write (style); -   AResp. Write ("<table class= ' table ' ><tr><th> name </th><th> Birth date </th><th> birthplace </th ><th> Graduation Time </th></tr>"); +             theSystem.Data.DataTable Dtsource =NewSystem.Data.DataTable (); -Dtsource.tablename ="Statistic"; $DTSOURCE.COLUMNS.ADD ("first column"); theDTSOURCE.COLUMNS.ADD ("second column"); theDTSOURCE.COLUMNS.ADD ("third column"); theDTSOURCE.COLUMNS.ADD ("Fourth Column"); the  -System.Data.DataRow row =NULL; inrow =Dtsource.newrow (); therow[0] ="Zhang San"; therow[1] ="1987-09-09"; Aboutrow[2] ="Baoding, Hebei"; therow[3] ="2008 Graduation"; the dtSource.Rows.Add (row); the  +row =Dtsource.newrow (); -row[0] ="John Doe"; therow[1] ="1987-09-02";Bayirow[2] ="Wuhan, Hubei"; therow[3] ="2009 Graduation"; the dtSource.Rows.Add (row); -  -row =Dtsource.newrow (); therow[0] ="Harry"; therow[1] ="1987-09-01"; therow[2] ="Xiangtan, Hunan"; therow[3] ="2013 Graduation"; - dtSource.Rows.Add (row); the  the             foreach(DataRow Tmprowinchdtsource.rows) the             {94Resp. Write ("<tr><td>"+ tmprow[0] +"</td>"); theResp. Write ("<td>"+ tmprow[1] +"</td>"); theResp. Write ("<td>"+ tmprow[2] +"</td>"); theResp. Write ("<td>"+ tmprow[3] +"</td>");98Resp. Write ("</tr>"); About             } -Resp. Write ("<table>");101 102 resp. Flush ();103 resp. End ();104         } the   106     }107}

JS section Content

1 $ ("#Excel"). Bind ('click', function () { 2                 var " ajaxhandler/radiofamilydaynumber.ashx? Action=excel"; 3                 window.location.href = URL; // Export Excel 4             })

ASP. Handler Export data to Excel (let users download)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.