无为清净楼资源网 Design By www.qnjia.com

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中的具体实现方法

标签:
Excel,存储,SQL

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。