中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在Asp.net項目中將Excel數據保存到SQL Server中

發布時間:2021-01-28 09:29:51 來源:億速云 閱讀:183 作者:Leah 欄目:開發技術

如何在Asp.net項目中將Excel數據保存到SQL Server中?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

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 = "程序出錯,請您檢查需要導入的表!";
            }
        }

關于如何在Asp.net項目中將Excel數據保存到SQL Server中問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

栾川县| 温宿县| 广安市| 赣州市| 文化| 双江| 新化县| 阳高县| 循化| 昌图县| 铁岭市| 个旧市| 柘城县| 阳高县| 龙州县| 甘孜| 烟台市| 油尖旺区| 深泽县| 闽侯县| 柳江县| 黄石市| 巴中市| 秦皇岛市| 安乡县| 泸州市| 万源市| 托里县| 达日县| 六枝特区| 宜宾县| 渑池县| 浦东新区| 双峰县| 卫辉市| 乐亭县| 万山特区| 临清市| 石家庄市| 都江堰市| 靖江市|