ExportHandler.ashx
using KYZWeb.Common;
using Liger.Data;
//using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace KYZWeb.Modules.DeviceMan
{
/// <summary>
/// ExportHandler 的摘要说明
/// </summary>
public class ExportHandler : IHttpHandler
{
public static DbContext DB = DbHelper.Db;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string startdatetime = context.Request.Params["startdatetime"];
string enddatetime = context.Request.Params["enddatetime"];
string user_name = context.Request.Params["user_name"];
StringBuilder sql = new StringBuilder();
sql.Append(@" select * ");
sql.Append(" from dbo.vi_card_run WHERE 1=1");
if (!string.IsNullOrEmpty(startdatetime))
{
sql.Append(" and card_datetime >= '" + startdatetime + "'");
}
if (!string.IsNullOrEmpty(enddatetime))
{
sql.Append(" and card_datetime <= '" + enddatetime + "'");
}
if (!string.IsNullOrEmpty(user_name))
{
sql.Append(" and user_name like '%" + user_name + "%'");
}
sql.Append(" order by card_datetime desc");
//创建command
var dt = DB.Db.ExecuteDataSet(CommandType.Text, sql.ToString()).Tables[0];
StringWriter sw = new StringWriter();
//sw.WriteLine("姓名\t工号\t打卡时间\t打卡地点\t打卡设备");
sw.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<head>");
sw.WriteLine("<!--[if gte mso 9]>");
sw.WriteLine("<xml>");
sw.WriteLine(" <x:ExcelWorkbook>");
sw.WriteLine(" <x:ExcelWorksheets>");
sw.WriteLine(" <x:ExcelWorksheet>");
sw.WriteLine(" <x:Name>" + "sheetName" + "</x:Name>");
sw.WriteLine(" <x:WorksheetOptions>");
sw.WriteLine(" <x:Print>");
sw.WriteLine(" <x:ValidPrinterInfo />");
sw.WriteLine(" </x:Print>");
sw.WriteLine(" </x:WorksheetOptions>");
sw.WriteLine(" </x:ExcelWorksheet>");
sw.WriteLine(" </x:ExcelWorksheets>");
sw.WriteLine("</x:ExcelWorkbook>");
sw.WriteLine("</xml>");
sw.WriteLine("<![endif]-->");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine("<table border='1'>");
sw.WriteLine(" <tr >");
sw.WriteLine(" <td><strong>姓名</strong></td>");
sw.WriteLine(" <td><strong>工号</strong></td>");
sw.WriteLine(" <td><strong>打卡时间</strong></td>");
sw.WriteLine(" <td><strong>打卡地点</strong></td>");
sw.WriteLine(" <td><strong>打卡设备</strong></td>");
sw.WriteLine(" </tr>");
foreach (DataRow dr in dt.Rows)
{
//sw.WriteLine(dr["user_name"] + "\t" + dr["user_code"] + "\t" + dr["card_datetime"]+"\t"+dr["card_address"]+"\t"+dr["card_device_name"]);
sw.WriteLine(" <tr>");
sw.WriteLine(" <td>" + dr["user_name"] + "</td>");
sw.WriteLine(" <td>" + dr["user_code"] + "</td>");
sw.WriteLine(" <td>" + dr["card_datetime"] + "</td>");
sw.WriteLine(" <td>" + dr["area_name"] + "</td>");
sw.WriteLine(" <td>" + dr["device_name"] + "</td>");
sw.WriteLine(" </tr>");
}
sw.WriteLine("</table>");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
sw.Close();
context.Response.Clear();
context.Response.Buffer = true;
context.Response.Charset = "UTF-8";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + "card" + ".xls");
context.Response.ContentType = "application/ms-excel";
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
context.Response.Write(sw);
context.Response.End();
//context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//context.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("card", System.Text.Encoding.UTF8) + ".xls");
//context.Response.ContentType = "application/ms-excel";
//context.Response.Write(sw);
//context.Response.End();
//DataTabletoExcel(dt, "C:\\中国.XLS"); //调用自定义的函数,当然输出文件你可以随便写
}
public static void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0;
//Application xlApp = new ApplicationClass();
//xlApp.DefaultFilePath = "";
//xlApp.DisplayAlerts = true;
//xlApp.SheetsInNewWorkbook = 1;
//Workbook xlBook = xlApp.Workbooks.Add(true);
////将DataTable的列名导入Excel表第一行
//foreach (DataColumn dc in tmpDataTable.Columns)
//{
// columnIndex++;
// xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;
//}
////将DataTable中的数据导入Excel中
//for (int i = 0; i < rowNum; i++)
//{
// rowIndex++;
// columnIndex = 0;
// for (int j = 0; j < columnNum; j++)
// {
// columnIndex++;
// xlApp.Cells[rowIndex, columnIndex] = tmpDataTable.Rows[i][j].ToString();
// }
//}
////xlBook.SaveCopyAs(HttpUtility.UrlDecode(strFileName, System.Text.Encoding.UTF8));
//xlBook.SaveCopyAs(strFileName);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
ExportHandler.ashx的更多相关文章
- Visual Studio 2013 添加一般应用程序(.ashx)文件到SharePoint项目
默认,在用vs2013开发SharePoint项目时,vs没有提供一般应用程序(.ashx)的项目模板,本文解决此问题. 以管理员身份启动vs2013,创建一个"SharePoint 201 ...
- ashx中Response.ContentType的常用类型
ashx中Response.ContentType的常用类型: text/plaintext/htmltext/xmlapplication/jsonimage/GIFapplication/x-cd ...
- 一种开发模式:ajax + ashx + UserControl
一.ajax+ashx模式的缺点 在web开发过程中,为了提高网站的用户体验,或多或少都会用到ajax技术,甚至有的网站全部采用ajax来实现,大量使用ajax在增强用户体验的同时会带来一些负 ...
- 如何使用VS在SharePont 2013中插入ashx文件
http://www.lifeonplanetgroove.com/adding-and-deploying-generic-handlers-ashx-to-a-sharepoint-2010-vi ...
- 分享一个html+js+ashx+easyui+ado.net权限管理系统
EasyUI.权限管理 这是个都快被搞烂了的组合,但是easyui的确好用,权限管理在项目中的确实用.一直以来博客园里也不少朋友分享过,但是感觉好的要不没源码,要不就是过度设计写的太复杂看不懂,也懒得 ...
- 在一个aspx或ashx页面里进行多次ajax调用
在用ajax开发asp.net程序里.利用ashx页面与前台页面进行数据交互.但是每个ajax交互都需要一个ashx页面.结果是项目里一大堆ashx页面.使项目难以管理.现在我们就想办法让一个ashx ...
- 在ashx文件中制作验证码(使用session要继承IRequiresSessionState)
必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写! System.Web.SessionState的一些接口 IRea ...
- jsonp跨域+ashx(示例)
前言 做B/S项目的时候,我们一般使用jquery+ashx来实现异步的一些操作,比如后台获取一些数据到前台,但是如果ashx文件不在本项目下,引用的是别的域下的文件,这时候就访问不了.关于jsonp ...
- ztree + ashx +DataTable +Oracle
问题描述 好久没有使用ztree了,刚才在使用ztree做导航时遇到了几个小问题: 1.返回数据源是undefined . 2.数据出现后树结构没有出现(pIdKey单词拼写错误). 3.在使用Ora ...
随机推荐
- 初学Python——文件操作
一.文件的打开和关闭 1.常用的打开关闭语句 f=open("yesterday","r",encoding="utf-8") #打开文件 ...
- DAO层基础设计原理
在实际的开发中有一种项目的程序组织架构方案叫做MVC模式,按照程序 的功能将他们分成三个层,如下图 Modle层(模型层).View层(显示层).Controller层(控制层). Modle层:可以 ...
- 洛谷 P1706 全排列问题
题目链接 https://www.luogu.org/problemnew/show/P1706 题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数 ...
- 开发手记:JedisConnectionException: Could not get a resource from the pool
对于Redis,生产环境是集群模式,测试环境是单例模式,如果在生产环境中用单例模式会报错. 解决办法,通过云配置,将配置进行自动化配置. 另附一份Redis配置: #***************** ...
- [SDOI2017]天才黑客[最短路、前缀优化建图]
题意 一个 \(n\) 点 \(m\) 边的有向图,还有一棵 \(k\) 个节点的 trie ,每条边上有一个字符串,可以用 trie 的根到某个节点的路径来表示.每经过一条边,当前携带的字符串就会变 ...
- SpringBoot分布式 - Dubbo+ZooKeeper
一:介绍 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务.它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护.域名服务.分布式同步.组服务等. Dubbo是Alib ...
- Node.js配合jQuery UI autocomplete的应用
Node.js擅长的领域为: 不需要很多运算 吞吐量要求高 进消息轻并且要求快 出消息轻并且要求快 网上的例子都是socket.io的,我一直在想到底能用在什么地方?根据node.js的优点(擅长领域 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(92)-打印EasyUI 的datagrid表格
前言 应用系统有时候需要打印Datagrid的表格内容,我们本节就来学习打印datagrid内容 打印主要使用:web打印(我们之前有讲过web打印插件jqprint) + 将datagrid重新编制 ...
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 客户端多网络支持
客户端可以支持灵活的,中间层连接选择,由于我们系统的定位架构大型信息系统的,所以全国各地,甚至国外的用户也会有,所以需要支持全网络配置,只要配置了中间层,可以选择连接哪个中间层的服务程序.客户端可以进 ...
- logstash安装及基础入门
Logstash是一款开源的数据收集引擎,具备实时管道处理能力.简单来说,logstash作为数据源与数据存储分析工具之间的桥梁,结合 ElasticSearch以及Kibana,能够极大方便数据的处 ...