Asp.net 에서 Excel 데 이 터 를 SQL Server 에 저장 하 는 구체 적 인 실현 방법

6898 단어 Excel기억SQL

ExcelWrapper

        /// <summary>
        /// EXCEL DATASET
        /// </summary>
        /// <param name="filenameurl"> </param>
        /// <param name="table">dataset ( )</param>
        /// <returns></returns>
        public static 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);
            conn.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
            odda.Fill(ds, table);
            return ds;
        }


.cs

       //
        protected void imgbtnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (!FileUpload1.HasFile)
                {
                    JsHelper.Alert(" Excel ", this);
                    return;
                }

                //
                string extension = System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();
                if (extension != ".xls" && extension != ".xlsx")
                {
                    JsHelper.Alert(" Excel ", this);
                    return;
                }

                //  Exel , SaveAs
                string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
                string savePath = Server.MapPath(("~\\upfiles\\") + filename);
                FileUpload1.SaveAs(savePath);

                DataSet ds = ExcelWrapper.ExecleDs(savePath, filename);
                DataRow[] dr = ds.Tables[0].Select();
                int rowsnum = ds.Tables[0].Rows.Count;
                List<String> lstMsg = new List<string>();
                if (rowsnum == 0)
                {
                    JsHelper.Alert("Excel , ", this);
                }
                else
                {
                    for (int i = 0; i < dr.Length; i++)
                    {
                        String error = "";

                        // excel
                        string num = dr[i][" "].ToString();
                        string name = dr[i][" "].ToString();
                        string pwd = dr[i][" "].ToString();
                        string collegeNum = dr[i][" "].ToString();
                        string birth = dr[i][" "].ToString();

                        if (!BLL.M_CollegeBLL.GetAllCollegeNum().Contains(collegeNum))
                        {
                            error += " &nbsp;";
                        }

                        if (String.IsNullOrEmpty(collegeNum))
                        {
                            error += " &nbsp;";
                        }

                        if (String.IsNullOrEmpty(num))
                        {
                            error += " &nbsp;";
                        }
                        else if (!Utility.IsLetterThanSomeLength(num, 25))
                        {
                            error += " &nbsp;";
                        }

                        if (String.IsNullOrEmpty(name))
                        {
                            error += " &nbsp;";
                        }
                        else if (!Utility.IsLetterThanSomeLength(name, 25))
                        {
                            error += " &nbsp;";
                        }

                         if (String.IsNullOrEmpty(birth))
                        {
                            error += " &nbsp;";
                        }
                        else if (!Utility.IsDateTime(birth))
                        {
                            error += " &nbsp;";
                        }
                        if (String.IsNullOrEmpty(sex))
                        {
                            error += " &nbsp;";
                        }
                        if (String.IsNullOrEmpty(error))
                        {
                            M_Student stu = new M_Student();
                            stu.Num = num;
                            stu.Name = name;
                            stu.Pwd = pwd;
                            stu.CollegeNum = collegeNum;
                            stu.Birthday = Convert.ToDateTime(birth);

                            //
                            if (!BLL.M_StudentBLL.GetAllStuNum().Contains(num))
                            {
                                BLL.M_StudentBLL.Add(stu);
                            }
                            else
                            {
                                BLL.M_StudentBLL.Modify(stu);
                            }
                        }
                        else
                        {
                            lstMsg.Add(" " + num + " ," + " :" + error + "。");
                        }
                    }
                }
                this.lblHint.Text = " 。";
                if (null != lstMsg)
                {
                    this.lblHint.Text += " " + lstMsg.Count() + " 。<br /><br />";
                    foreach (string s in lstMsg)
                    {
                        this.lblHint.Text += s;
                    }
                }
            }
            catch
            {
                this.lblHint.Text = " , !";
            }
        }

효과 도

좋은 웹페이지 즐겨찾기