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的更多相关文章

  1. Visual Studio 2013 添加一般应用程序(.ashx)文件到SharePoint项目

    默认,在用vs2013开发SharePoint项目时,vs没有提供一般应用程序(.ashx)的项目模板,本文解决此问题. 以管理员身份启动vs2013,创建一个"SharePoint 201 ...

  2. ashx中Response.ContentType的常用类型

    ashx中Response.ContentType的常用类型: text/plaintext/htmltext/xmlapplication/jsonimage/GIFapplication/x-cd ...

  3. 一种开发模式:ajax + ashx + UserControl

    一.ajax+ashx模式的缺点     在web开发过程中,为了提高网站的用户体验,或多或少都会用到ajax技术,甚至有的网站全部采用ajax来实现,大量使用ajax在增强用户体验的同时会带来一些负 ...

  4. 如何使用VS在SharePont 2013中插入ashx文件

    http://www.lifeonplanetgroove.com/adding-and-deploying-generic-handlers-ashx-to-a-sharepoint-2010-vi ...

  5. 分享一个html+js+ashx+easyui+ado.net权限管理系统

    EasyUI.权限管理 这是个都快被搞烂了的组合,但是easyui的确好用,权限管理在项目中的确实用.一直以来博客园里也不少朋友分享过,但是感觉好的要不没源码,要不就是过度设计写的太复杂看不懂,也懒得 ...

  6. 在一个aspx或ashx页面里进行多次ajax调用

    在用ajax开发asp.net程序里.利用ashx页面与前台页面进行数据交互.但是每个ajax交互都需要一个ashx页面.结果是项目里一大堆ashx页面.使项目难以管理.现在我们就想办法让一个ashx ...

  7. 在ashx文件中制作验证码(使用session要继承IRequiresSessionState)

    必须继承System.Web.SessionState.IRequiresSessionState接口,才能实现Session读写! System.Web.SessionState的一些接口 IRea ...

  8. jsonp跨域+ashx(示例)

    前言 做B/S项目的时候,我们一般使用jquery+ashx来实现异步的一些操作,比如后台获取一些数据到前台,但是如果ashx文件不在本项目下,引用的是别的域下的文件,这时候就访问不了.关于jsonp ...

  9. ztree + ashx +DataTable +Oracle

    问题描述 好久没有使用ztree了,刚才在使用ztree做导航时遇到了几个小问题: 1.返回数据源是undefined . 2.数据出现后树结构没有出现(pIdKey单词拼写错误). 3.在使用Ora ...

随机推荐

  1. Spring Boot配置文件详解-ConfigurationProperties和Value优缺点-(转)好文

    文章转自 http://www.cnblogs.com/itdragon/p/8686554.html Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件.他们 ...

  2. 从Linux内核角度看中间人攻击(ARP欺骗)并利用Python scapy实现

    邻居子系统与ARP协议 邻居子系统的作用就是将IP地址,转换为MAC地址,类似操作系统中的MMU(内存管理单元),将虚拟地址,转换为物理地址. 其中邻居子系统相当于地址解析协议(IPv4的ARP协议, ...

  3. JavaScript的基本包装类型说明

    一.基本包装类型: 为了便于操作基本类型值,ECMAScript 还提供了3个特殊的引用类型:Boolean.Number和String.这些基本包装类型,具有与各自基本类型相应的特殊行为. 实际上我 ...

  4. Java IO(三)——字节流

    一.流类 Java的流式输入/输出是建立在四个抽象类的基础上的:InputStream.OutputStream.Reader.Writer.它们用来创建具体的流式子类.尽管程序通过具体子类执行输入/ ...

  5. .net core实践系列之短信服务-架构优化

    前言 通过前面的几篇文章,讲解了一个短信服务的架构设计与实现.然而初始方案并非100%完美的,我们仍可以对该架构做一些优化与调整. 同时我也希望通过这篇文章与大家分享一下,我的架构设计理念. 源码地址 ...

  6. Java类的加载及实例的创建

    java中class.forName()和classLoader都可用来对类进行加载.class.forName()前者除了将类的.class文件加载到jvm中之外,还会对类进行解释,执行类中的sta ...

  7. 一次linux服务器黑客入侵后处理

     场景: 周一上班centos服务器ssh不可用,web和数据库等应用不响应.好在vnc可以登录 使用last命令查询,2号之前的登录信息已被清空,并且sshd文件在周六晚上被修改,周日晚上2点服务器 ...

  8. new、getInstance()、newInstance()、Class.forName()

    1.对象使用之前通过getinstance()得到而不需要自己定义,用完之后不需要delete: 2.new 一定要生成一个新对象,分配内存:getInstance() 则不一定要再次创建,它可以把一 ...

  9. react虚拟dom diff算法

    react虚拟dom:依据diff算法 前端:更新状态.更新视图:所以前端页面的性能问题主要是由Dom操作引起的,解放Dom操作复杂性 刻不容缓 因为:Dom渲染慢,而JS解析编译相对非常非常非常快! ...

  10. c++入门之—运算符重载和友元函数

    运算符重载的意义是:将常见的运算符重载出其他的含义:比如将*重载出指针的含义,将<<与cout联合使用重载出输出的含义,但需要认识到的问题是:运算符的重载:本质仍然是成员函数,即你可以认为 ...