<%@ WebHandler Language="C#" Class="excel" %>

using System;
using System.Web;
using SystemFramework;
using WriteExcel;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DataLinkMesWork;

public class excel : IHttpHandler {

    HttpContext contextMesWebFun;
    public void ProcessRequest (HttpContext context) {
        string ss;
        DataLinkMesWork.DataLink.InitSystem(out ss);
        //if (!InitSystemReg(out resultReg))
        //{
        //    return resultReg;
        //}
        contextMesWebFun = context; ;
        context.Response.ContentType = "text/plain";
        string type = contextMesWebFun.Request["type"];
        string responseText = "";
             byte[] bytes=null;

        switch (type)
        {
            //扫描信息
            case "Excel_NewArrivalList":
          bytes=  Excel_NewArrivalList();
              HttpContext.Current.Response.ContentType = "application/ms-excel";
            //通知浏览器下载文件而不是打开  
            HttpContext.Current.Response.AddHeader("Content-Disposition", "filename="+ HttpUtility.UrlEncode("OutsourcingMaterialsSearch.xls", System.Text.Encoding.UTF8));
            HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
                break;
        }
    }
    /// <summary>
    ///  外购模板
    /// </summary>
    /// <returns></returns>
    public  byte[] Excel_NewArrivalList()
    {
        string value = null;
            byte[] bytes=null;
        try
        {
            System.Data.DataTable dt = null;
            string folename = HttpContext.Current.Request["folename"];
            string rec1 = HttpContext.Current.Request["rec1"];
            string rec2 = HttpContext.Current.Request["rec2"];
            string rec3 = HttpContext.Current.Request["rec3"];
            string rec4 = HttpContext.Current.Request["rec4"];
            List<string> str = new List<string>();
            str.Add(rec1);
            str.Add(rec2);
            str.Add(rec3);
            str.Add(rec4);
            Hashtable ht=null;
            pushHtDt1(str,out ht);
            string sql = HttpContext.Current.Request["sql"];
            Excel_NewArrivalList_SQL( out dt);
            WriteExcel. Form1 frm = new Form1();
             bytes  = frm.demo1(folename, ht, dt);
        }
        catch (Exception err)
        {
        }
            return bytes;

    }
    // 将固定参数传入哈希表
    private void pushHtDt1(List<string> str ,out Hashtable htDt1)
    {
        htDt1 = new Hashtable();
        for (int i = 0; i < str.Count; i++)
        {
            htDt1.Add(i, str[i].ToString());
        }
    }


    static public void Excel_NewArrivalList_SQL(out DataTable dt)
    {
        ParamData[] paramData = new ParamData[1];
        paramData[0].name = "到货单流水号";
        paramData[0].value = "39";


        string procedureName = "备料管理_到货单明细查询";
        SqlParameter[] thisParms = new SqlParameter[1];
        thisParms[0] = new System.Data.SqlClient.SqlParameter("@到货单流水号", "39");
        SqlCmd.ExecuteStoredProcedure(procedureName, ref  paramData, out dt);
    }

    //public static bool ExecuteStoredProcedure(string procedureName, ref SqlParameter[] sqlParameters, out DataTable dt)
    //{
    //    string errorMessage;
    //    DataSet ds;
    //    ExecuteStoredProcedure(procedureName, "server=192.168.1.140;database=ERPTOOL_GJ;uid=sa;pwd=126.com;Connection Reset=FALSE;Max Pool Size = 1000", ref sqlParameters,out ds, out  errorMessage);
    //    try
    //    {
    //        dt = ds.Tables[0];

    //        return true;
    //    }
    //    catch
    //    {
    //        dt = null;
    //        return false;

    //    }
    //}
    //public static bool ExecuteStoredProcedure(string procedureName, string connectionString, ref SqlParameter[] sqlParameters, out DataSet ds, out string errorMessage)
    //{
    //    bool result = false;
    //    errorMessage = "";
    //    ds = new DataSet();
    //    using (SqlConnection conn = new SqlConnection(connectionString))
    //    {
    //        try
    //        {
    //            conn.Open();
    //            using (SqlDataAdapter dsCommand = new SqlDataAdapter())
    //            {
    //                dsCommand.SelectCommand = new SqlCommand(procedureName, conn);
    //                dsCommand.SelectCommand.CommandType = CommandType.StoredProcedure;
    //                for (int i = 0; i < sqlParameters.Length; i++)
    //                {
    //                    dsCommand.SelectCommand.Parameters.Add(sqlParameters[i]);
    //                }
    //                dsCommand.Fill(ds);
    //            }
    //            result = true;
    //        }
    //        catch (Exception e)
    //        {
    //            errorMessage = e.ToString();
    //            //TestConnection();
    //            ApplicationLog.WriteLog(e, "\r\nExecuteStoredProcedure\r\n存储过程：\r\n" + procedureName + "\r\n连接字符串：\r\n" + connectionString);
    //        }
    //        finally
    //        {
    //            if (conn.State == ConnectionState.Open)
    //                conn.Close();
    //        }
    //    }
    //    return result;
    //}
    public bool IsReusable {
        get {
            return false;
        }
    }

}


