asp.net 通过 Handler 导出数据至excel (让用户下载)
效果图:
代码:
Export2Excel.ashx
<%@ WebHandler Language="C#" CodeBehind="Export2Excel.ashx.cs" Class="BLIC.SecurityCodeValidate.Web.Handler.Export2Excel" %>
Export2Excel.ashx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.IO;
using System.Data; namespace BLIC.SecurityCodeValidate.Web.Handler
{
/// <summary>
/// AdminLogin 的摘要说明
/// </summary>
public class Export2Excel : IHttpHandler, IRequiresSessionState
{ public void ProcessRequest(HttpContext context)
{ try
{
test1(context);
}
catch (Exception ex)
{
} //try
//{
// test1(context);
//}
//catch (Exception ex)
//{
// //context.Response.ContentType = "text/plain";
// context.Response.Write("导出失败:" + ex.Message);
//}
} public bool IsReusable
{
get
{
return false;
}
} private void test1(HttpContext context)
{
HttpResponse resp = System.Web.HttpContext.Current.Response;
resp.Charset = "utf-8";
resp.Clear();
string filename = "统计贴标报表_" + DateTime.Now.ToString("yyyyMMddHHmmss");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
resp.ContentEncoding = System.Text.Encoding.UTF8; resp.ContentType = "application/ms-excel";
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>";
resp.Write(style); resp.Write("<table class='table'><tr><th>姓名</th><th>出生年月</th><th>籍贯</th><th>毕业时间</th></tr>"); System.Data.DataTable dtSource = new System.Data.DataTable();
dtSource.TableName = "statistic";
dtSource.Columns.Add("第一列");
dtSource.Columns.Add("第二列");
dtSource.Columns.Add("第三列");
dtSource.Columns.Add("第四列"); System.Data.DataRow row = null;
row = dtSource.NewRow();
row[0] = "张三";
row[1] = "1987-09-09";
row[2] = "河北保定";
row[3] = "2008年毕业";
dtSource.Rows.Add(row); row = dtSource.NewRow();
row[0] = "李四";
row[1] = "1987-09-02";
row[2] = "湖北武汉";
row[3] = "2009年毕业";
dtSource.Rows.Add(row); row = dtSource.NewRow();
row[0] = "王五";
row[1] = "1987-09-01";
row[2] = "湖南湘潭";
row[3] = "2013年毕业";
dtSource.Rows.Add(row); foreach (DataRow tmpRow in dtSource.Rows)
{
resp.Write("<tr><td>" + tmpRow[0] + "</td>");
resp.Write("<td>" + tmpRow[1] + "</td>");
resp.Write("<td>" + tmpRow[2] + "</td>");
resp.Write("<td>" + tmpRow[3] + "</td>");
resp.Write("</tr>");
}
resp.Write("<table>"); resp.Flush();
resp.End();
} }
}
asp.net 通过 Handler 导出数据至excel (让用户下载)的更多相关文章
- ASP.NET导出数据到Excel 实例介绍
ASP.NET导出数据到Excel 该方法只是把asp.net页面保存成html页面只是把后缀改为xlc不过excel可以读取,接下连我看看还有别的方式能导出数据,并利用模版生成. 下面是代码 新建 ...
- ASP导出数据到excel遇到的一些问题
一直用动易平台的ASP做新闻发布网站,直到现在才接触导出数据到Excel的问题,目的在于公司要统计各部门的投稿量,要做这么个东西,实现起来是挺简单的,但是第一次做,还是费了一些功夫的,特此记录一下 主 ...
- 1.ASP.NET MVC使用EPPlus,导出数据到Excel中
好久没写博客了,今天特地来更新一下,今天我们要学习的是如何导出数据到Excel文件中,这里我使用的是免费开源的Epplus组件. 源代码下载:https://github.com/caofangshe ...
- NPOI导出数据到Excel
NPOI导出数据到Excel 前言 Asp.net操作Excel已经是老生长谈的事情了,可下面我说的这个NPOI操作Excel,应该是最好的方案了,没有之一,使用NPOI能够帮助开发者在没有安装微 ...
- 导出数据到Excel表格
开发工具与关键技术:Visual Studio 和 ASP.NET.MVC,作者:陈鸿鹏撰写时间:2019年5月25日123下面是我们来学习的导出数据到Excel表格的总结首先在视图层写导出数据的点击 ...
- 导出数据到Excel --使用ExcelReport有感
先看图,这是几个月前用NPOI写的导出数据到Excel,用了上百行代码,而且难控制,导出来也比较难看 excel打开的效果 下面是我用ExcelReport类库导出到Excel的操作 1.首先引用Ex ...
- 使用Open xml 操作Excel系列之二--从data table导出数据到Excel
由于Excel中提供了透视表PivotTable,许多项目都使用它来作为数据分析报表. 在有些情况下,我们需要在Excel中设计好模板,包括数据源表,透视表等, 当数据导入到数据源表时,自动更新透视表 ...
- Dynamics CRM导出数据到Excel
原创地址:http://www.cnblogs.com/jfzhu/p/4276212.html 转载请注明出处 Pivot Table是微软BI的一个重要工具,所以这里讲一下Dynamics CRM ...
- MVC导出数据到EXCEL新方法:将视图或分部视图转换为HTML后再直接返回FileResult
导出EXCEL方法总结 MVC导出数据到EXCEL的方法有很多种,常见的是: 1.采用EXCEL COM组件来动态生成XLS文件并保存到服务器上,然后转到该文件存放路径即可: 优点:可设置丰富的EXC ...
随机推荐
- Oracle查询数据中占用空间最大的表
--第一步,查询istaudit数据库文件ID,文件路径 select file#,name from v$datafile where lower(name) like '%istaudit.dbf ...
- DLL运行时动态加加载的问题
1.error C2440: 'initializing' : cannot convert from 'int (__stdcall *)(void)' to 'void (__cdecl *)(c ...
- [LeetCode]题解(python):016-3Sum Closest
题目来源: https://leetcode.com/problems/3sum-closest/ 题意分析: 这道题目输入一个数组nums和一个数target,找出数组中三个数,使得他们的和最接近t ...
- Qt分析:Qt中的两种定时器(可是QObject为什么要提高定时器呢,没必要啊。。。)
Qt有两种定时器,一种是QObject类的定时器,另一种是QTimer类的定时器. (1)QObject类的定时器 QObject类提供了一个基本的定时器,通过函数startTimer()来启 ...
- asp.net插入sql server 中文乱码问题解决方案
创建数据库的代码---创建promary表 create table promary ( proID int primary key, proName varchar(50) not null ) 出 ...
- python and 和 or
在Python 中,and 和 or 执行布尔逻辑演算,如你所期待的一样.但是它们并不返回布尔值,而是返回它们实际进行比较的值之一. 例 4.15. and 介绍 >>> 'a' a ...
- Physiological Processes of Speech Production--Reading Notes (8)
Upper Jaw The upper jaw, or the maxilla with the upper teeth, is the structure fixed to the skull, f ...
- c# datagridviewcomboboxcell值无效的解决办法
一直认为是数据库存储的数据和datagridviewcomboboxcell对不上导致,今天碰到两者对应上了,预览的时候还是提示错误, 查看了下网上其他大神的解决方法,是数据库字段类型有误,查看了下, ...
- iOS导航条渐变透明
来源:HelloYeah 链接:http://www.jianshu.com/p/b8b70afeda81 下面这个界面有没有觉得很眼熟.打开你手里的App仔细观察,你会发现很多都有实现这个功能.比如 ...
- 怎样让jQuery和其它js库共存
怎样让jQuery和其它js库共存 有时候需要同时使用jQuery和其它javascript,比如在joomla中默认的是motools,但很多人还是希 望能够使用jQuery,如果直接调用的话,由于 ...