• c#导入EXCEL数据


    using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Drawing; using System.Data; using System.IO; using System.Data.OleDb; namespace Web_HengliSeed.Sys_Manage {     public partial class M_UpLoadFile1 : System.Web.UI.Page     {         //string ifpub = "";         DataTable dt1 = new DataTable();         protected void Page_Load(object sender, EventArgs e)         {             ClassFile.Function.CheckState();             if (!IsPostBack)             {                 HttpPostedFile txt_file = Request.Files["Filedata"];

                    if (txt_file.FileName != "")                 {                     SqlConnection conn = new SqlConnection(ClassFile.SqlHelper.ConnectionString);                     string strClassID = Common.GetMes.GetRequestQuery(Request, "AID");                     string newfilename = BLL.UpFile.GetUniqueString();                     //string newfilename = BLL.UpFile.SaveFile(File1, Server.MapPath(@"Sendtxt/upload/"));                     txt_file.SaveAs(Server.MapPath(@"Sendtxt/upload/") + newfilename+".xls");                     dt1.Columns.Add("Name", typeof(string));                     dt1.Columns.Add("Mobile", typeof(string));                     dt1.Columns.Add("Series", typeof(string));                     dt1.Columns.Add("PlateNumber", typeof(string));                     dt1.Columns.Add("AddTime", typeof(string));                     string opnFileName = Server.MapPath(@"Sendtxt/upload/") + newfilename + ".xls";                     if (File.Exists(opnFileName) == false)                     {                         return;                     }                     string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="          + opnFileName + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";                     OleDbConnection connexcel = new OleDbConnection(strConn);                     string strExcel = "";                     OleDbDataAdapter myCommand = null;                     DataSet ds = new DataSet();                     strExcel = "select * from [sheet1$]";                     try                     {                         connexcel.Open();                         myCommand = new OleDbDataAdapter(strExcel, strConn);

                            myCommand.Fill(ds, "dtSource");                     }                     catch (Exception ex)                     {                         Response.Write("<script>alert('导入出错')</script>");                         throw ex;                     }                     finally                     {                         connexcel.Close();                         connexcel.Dispose();                     }                     DataTable dt = new DataTable();                     dt = ds.Tables[0];                     int allrows = dt.Rows.Count;                     for (int n = 0; n < allrows; n++)                     {                         if (BLL.Check.IFPhoneNumber(dt.Rows[n][1].ToString()) != -1)                         {                             object[] ob = new object[5];                             ob[0] = dt.Rows[n][0].ToString();                             ob[1] = dt.Rows[n][1].ToString();                             ob[2] = "";                             ob[3] = "";                             ob[4] = strClassID;                             dt1.Rows.Add(ob);                         }                     }                     BLL.UpFile.DeleteFile(Server.MapPath(@"Sendtxt/upload/"), newfilename);                     try                     {                         conn.Open();                         SqlCommand comm1 = new SqlCommand("SMS_pr_AddSMSTemp", conn);                         comm1.CommandType = CommandType.StoredProcedure;                         comm1.Parameters.Add(new SqlParameter("@TVP", dt1));

                            comm1.ExecuteNonQuery();                         conn.Close();

                                        return;                     }                     catch                     {                         ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('加入失败')", true);                         return;                     }                 }             }

            }

            protected void Button1_Click1(object sender, EventArgs e)         {             if (File1.Value != "")             {                 SqlConnection conn = new SqlConnection(ClassFile.SqlHelper.ConnectionString);                 string strClassID = Common.GetMes.GetRequestQuery(Request, "AID");                 if (!BLL.UpFile.IsAllowedExtension(File1))                 {                     Common.ShowMessage.Show(Page, "error", "只能上传txt文本");                     return;                 }                 //   string newfilename = BLL.UpFile.GetUniqueString();                 string newfilename = BLL.UpFile.SaveFile(File1, Server.MapPath(@"Sendtxt/upload/"));                 dt1.Columns.Add("Name", typeof(string));                 dt1.Columns.Add("Mobile", typeof(string));                 dt1.Columns.Add("Series", typeof(string));                 dt1.Columns.Add("PlateNumber", typeof(string));                 dt1.Columns.Add("AddTime", typeof(string));                 string opnFileName = Server.MapPath(@"Sendtxt/upload/") + newfilename;                 if (File.Exists(opnFileName) == false)                 {                     return;                 }                 string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="      + opnFileName + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";                 OleDbConnection connexcel = new OleDbConnection(strConn);                 string strExcel = "";                 OleDbDataAdapter myCommand = null;                 DataSet ds = new DataSet();                 strExcel = "select * from [sheet1$]";                 try                 {                     connexcel.Open();                     myCommand = new OleDbDataAdapter(strExcel, strConn);

                        myCommand.Fill(ds, "dtSource");                 }                 catch (Exception ex)                 {                     Response.Write("<script>alert('导入出错')</script>");                     throw ex;                 }                 finally                 {                     connexcel.Close();                     connexcel.Dispose();                 }                 DataTable dt = new DataTable();                 dt = ds.Tables[0];                 int allrows=dt.Rows.Count;                 for (int n = 0; n < allrows; n++)                 {                     if (BLL.Check.IFPhoneNumber(dt.Rows[n][1].ToString()) != -1)                     {                         object[] ob = new object[5];                         ob[0] = dt.Rows[n][0].ToString();                         ob[1] = dt.Rows[n][1].ToString();                         ob[2] = "";                         ob[3] = "";                         ob[4] = strClassID;                         dt1.Rows.Add(ob);                     }                 }                 BLL.UpFile.DeleteFile(Server.MapPath(@"Sendtxt/upload/"), newfilename);                 try                 {                     conn.Open();                     SqlCommand comm1 = new SqlCommand("SMS_pr_AddSMSTemp", conn);                     comm1.CommandType = CommandType.StoredProcedure;                     comm1.Parameters.Add(new SqlParameter("@TVP", dt1));

                        comm1.ExecuteNonQuery();                     conn.Close();

                        Response.Write("<script>self.close()</script>");                     return;                 }                 catch                 {                     ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('加入失败')", true);                     return;                 }                     //FileStream fs = new FileStream(Server.MapPath(@"Sendtxt/upload/" + newfilename), FileMode.Open, FileAccess.Read);                     //StreamReader sr = new StreamReader(fs);                     //string strLine = sr.ReadLine();                     //while (strLine != null)                     //{                     //    if (BLL.Check.IFPhoneNumber(strLine) != -1)                     //    {                     //        object[] ob = new object[5];                     //        ob[0] = "";                     //        ob[1] = strLine;                     //        ob[2] = "";                     //        ob[3] = "";                     //        ob[4] = strClassID;                     //        dt1.Rows.Add(ob);                     //    }                     //    strLine = sr.ReadLine();

                        //}

                        //sr.Close();                     //fs.Close();

                        //BLL.UpFile.DeleteFile(Server.MapPath(@"Sendtxt/upload/"), newfilename);                     //try                     //{                     //    conn.Open();                     //    SqlCommand comm1 = new SqlCommand("SMS_pr_AddSMSTemp", conn);                     //    comm1.CommandType = CommandType.StoredProcedure;                     //    comm1.Parameters.Add(new SqlParameter("@TVP", dt1));

                        //    comm1.ExecuteNonQuery();                     //    conn.Close();

                        //    Response.Write("<script>self.close()</script>");                     //    return;                     //}                     //catch                     //{                     //    ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('加入失败')", true);                     //    return;                     //}

                   

                }         }

            protected void Button2_Click(object sender, EventArgs e)         {             Response.Write("<script>self.close()</script>");         }

            //    if (string.IsNullOrEmpty(strClassID))         //    {         //        try         //        {         //            SqlConnection conn = new SqlConnection(ClassFile.SqlHelper.ConnectionString);         //            conn.Open();         //            SqlCommand comm = new SqlCommand("SMS_pr_AddClient", conn);         //            comm.CommandType = CommandType.StoredProcedure;         //            comm.Parameters.Add(new SqlParameter("@Sex", pay));         //            comm.Parameters.Add(new SqlParameter("@Source", source));         //          comm.Parameters.Add(new SqlParameter("@ID", strClassID));         //        comm.Parameters.Add(new SqlParameter("@AddTime", addtime));         //        comm.Parameters.Add(new SqlParameter("@Staff", staff));         //        comm.Parameters.Add(new SqlParameter("@Name", name));         //        comm.Parameters.Add(new SqlParameter("@AName", aname));         //        comm.Parameters.Add(new SqlParameter("@Mobile", mobile));         //        comm.Parameters.Add(new SqlParameter("@Remark", remark));         //        comm.Parameters.Add(new SqlParameter("@CityID", cityid));

            //        comm.Parameters.Add(new SqlParameter("@IFDelete", ifdelete));         //          comm.Parameters.Add(new SqlParameter("@BuyTime", buytime));

            //         comm.Parameters.Add( new SqlParameter("@Brand", brand));         //          comm.Parameters.Add(new SqlParameter("@Series", series));         //         comm.Parameters.Add( new SqlParameter("@Model", mobile));         //       comm.Parameters.Add( new SqlParameter("@SellerID", Session["SellerID"].ToString()));           //            comm.ExecuteNonQuery();         //            conn.Close();         //            Common.ShowMessage.myScriptMes(Page, "Suess", "if(confirm(\"信息保存成功,是否继续添加?否则返回信息列表。\")) {location.href=\"M_EditClient.aspx\"} else  {location.href=\"M_ClientList.aspx\"} ");         //        }         //        catch         //        {         //            Common.ShowMessage.Show(Page, "error", "信息保存失败,请重试或联系技术人员......");         //        }         //        //if (BLL.WebLink.EditClientMes(pr))         //        //{         //        //    Common.ShowMessage.myScriptMes(Page, "Suess", "if(confirm(\"信息保存成功,是否继续添加?否则返回信息列表。\")) {location.href=\"M_EditClient.aspx\"} else  {location.href=\"M_ClientList.aspx\"} ");         //        //}         //        //else         //        //{         //        //    Common.ShowMessage.Show(Page, "error", "信息保存失败,请重试或联系技术人员......");         //        //}         //    }         //    else         //    {         //        //if (BLL.WebLink.EditClientMes(strClassID, pr))         //        //{         //        //    Common.ShowMessage.Show(Page, "suess", "信息保存成功......", "M_SeriesList.aspx");         //        //}         //        //else         //        //{         //        //    Common.ShowMessage.Show(Page, "error", "信息保存失败,请重试或联系技术人员......");         //        //}         //    }         //}

                } }

  • 相关阅读:
    抽丝剥茧,在实践中深入学习QTP
    项目实践精解:ASP.NET应用开发
    Oracle 10g宝典(第2版)
    PHP5应用实例详解
    项目实践精解:C#核心技术应用开发
    乐于分享、善待他人
    《jBPM4工作流应用开发指南》这本书
    IT项目的面向对象分析设计、开发及管理
    SPSS 17.0中文版常用功能与应用实例精讲
    TSQL 访问远程数据库并对其数据表进行操作
  • 原文地址:https://www.cnblogs.com/rambo1293271398/p/2652674.html
Copyright © 2020-2023  润新知