using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using CRM.BLL;
using CRM.Model;
using CRM.Common;


public partial class Pages_Client_ClientUpload : System.Web.UI.Page
{
    public DataSet ExecleDs(string filenameurl,string table)
    {
         
            string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" +filenameurl+ ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
           
            OleDbConnection conn = new OleDbConnection(strConn);
      
            OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]",conn);

            DataSet ds = new DataSet();

            odda.Fill(ds,table);
      
            return ds;

   }

    protected void Button1_Click(object sender, EventArgs e)
    {


        string IsXls = System.IO.Path.GetExtension(FileUpload1.FileName.ToString().ToLower());
        if (IsXls != ".xls")
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script type='text/javascript' language='javascript'>alert('只可以选择Excel文件')</script>");
            return;//当选择的不是Excel文件时,返回
        }
        string error = null;

        //string strpath = FileUpload1.PostedFile.FileName.ToString();         //获取Execle文件路径
        ////string filename = FileUpload1.FileName;        //获取Execle文件名
        //string filename = string.Format("yyyyMMdd-HH-MM",DateTime.Now.ToString())+strpath.Substring(strpath.LastIndexOf("\\") + 1);
        //string serverpath = Server.MapPath("Files/") +filename;   //这里设置文件的保存路径
        
        //FileUpload1.PostedFile.SaveAs(serverpath);

        string filepath = FileUpload1.PostedFile.FileName;
        string filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString()+filepath.Substring(filepath.LastIndexOf("\\") + 1);
        string serverpath = Server.MapPath("../../Files/") + filename;   //这里设置文件的保存路径 
        FileUpload1.PostedFile.SaveAs(serverpath);

        //DataSet ds = ExecleDs(strpath, filename);
        DataSet ds = ExecleDs(serverpath, filename);


        DataRow[] dr = ds.Tables[0].Select();          //定义一个DataRow数组
        int rowsnum = ds.Tables[0].Rows.Count;
        if (rowsnum == 0)
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<script type='text/javascript' language='javascript'>alert('Excel表为空表,无数据!')</script>");

            return;
        }
        else
        {
            for (int i = 0; i < dr.Length; i++)
            {
                ClientBasicInfo clientbasicinfo = new ClientBasicInfo();
                clientbasicinfo.客户名称 = dr[i]["客户名称"].ToString();
                clientbasicinfo.客户电话 = dr[i]["客户电话"].ToString();
                clientbasicinfo.主联系人 = dr[i]["主联系人"].ToString();
                clientbasicinfo.详细地址 = dr[i]["地址"].ToString();
                clientbasicinfo.电子邮箱 = dr[i]["电子邮箱"].ToString();
                clientbasicinfo.业务员 = Int32.Parse(Session["LoginID"].ToString());
                clientbasicinfo.CID = Int32.Parse(Session["CID"].ToString());
                clientbasicinfo.客户阶段 = CRM.Common.InitData.GetClientPhaseID(clientbasicinfo.CID,dr[i]["客户阶段"].ToString());
                clientbasicinfo.客户传真 = "-1";
                clientbasicinfo.联系手机 = "-1";
                clientbasicinfo.省份 = -1;
                clientbasicinfo.客户状态 = -1;
                clientbasicinfo.客户级别 = -1;
                clientbasicinfo.行业 = -1;
                clientbasicinfo.备注="-1";
                clientbasicinfo.部门 = "-1";
                clientbasicinfo.职务 = dr[i]["职务"].ToString();
                try
                {
                    clientbasicinfo.创建日期 = DateTime.Parse(dr[i]["注册日期"].ToString());
                }
                catch {
                    clientbasicinfo.创建日期 = DateTime.Now;
                }

                ClientBasic clientBasic = new ClientBasic();
                clientBasic.ClientBasicAdd(clientbasicinfo);

                Label1.Text = "添加成功";

            }

            Response.Write("<script>alert('Excle表导入成功!')</script>");

            Label1.Text = error;
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
