程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> Asp.net中把Excel數據存儲至SQL Server中的具體實現方法

Asp.net中把Excel數據存儲至SQL Server中的具體實現方法

編輯:ASP.NET基礎

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 += "所屬學院不存 ";
                        }

                        if (String.IsNullOrEmpty(collegeNum))
                        {
                            error += "請選擇該學生所在院系 ";
                        }

                        if (String.IsNullOrEmpty(num))
                        {
                            error += "學號不能為空 ";
                        }
                        else if (!Utility.IsLetterThanSomeLength(num, 25))
                        {
                            error += "學號的長度過長 ";
                        }

                        if (String.IsNullOrEmpty(name))
                        {
                            error += "姓名不能為空 ";
                        }
                        else if (!Utility.IsLetterThanSomeLength(name, 25))
                        {
                            error += "姓名的長度過長 ";
                        }

                         if (String.IsNullOrEmpty(birth))
                        {
                            error += "出生日期不能為空 ";
                        }
                        else if (!Utility.IsDateTime(birth))
                        {
                            error += "出生日期格式不正確 ";
                        }
                        if (String.IsNullOrEmpty(sex))
                        {
                            error += "性別不能為空 ";
                        }
                        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 = "程序出錯,請您檢查需要導入的表!";
            }
        }

效果圖

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved