主页 > 知识库 > Asp.net中把Excel数据存储至SQL Server中的具体实现方法

Asp.net中把Excel数据存储至SQL Server中的具体实现方法

热门标签:莱芜外呼电销机器人价格 智能电话营销外呼系统 五常地图标注 地图标注和认领 电销语音自动机器人 凯立德导航官网地图标注 户外地图标注软件手机哪个好用 郑州400电话办理 联通 长春呼叫中心外呼系统哪家好

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;
                ListString> lstMsg = new Liststring>();
                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数据导入到数据库的方法
  • ASP.NET使用GridView导出Excel实现方法
  • asp.net导出excel数据的常见方法汇总
  • Asp.net导出Excel/Csv文本格式数据的方法
  • Asp.Net使用Npoi导入导出Excel的方法
  • asp.net导出excel的简单方法实例
  • asp.net读取excel文件的三种方法示例
  • ASP.NET导出数据到Excel的实现方法
  • Asp.net中DataTable导出到Excel的方法介绍
  • asp.net中Table生成Excel表格的方法

标签:衢州 岳阳 福州 湖州 西宁 红河 宣城 西藏

巨人网络通讯声明:本文标题《Asp.net中把Excel数据存储至SQL Server中的具体实现方法》,本文关键词  Asp.net,中把,Excel,数据,存储,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《Asp.net中把Excel数据存储至SQL Server中的具体实现方法》相关的同类信息!
  • 本页收集关于Asp.net中把Excel数据存储至SQL Server中的具体实现方法的相关信息资讯供网民参考!
  • 推荐文章