最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:懂视网 责编:小采 时间:2020-11-27 22:40:51
文档

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

Asp.net中把Excel数据存储至SQL Server中的具体实现方法: ExcelWrapper 代码如下: /// <summary> /// 查询EXCEL电子表格添加到DATASET /// </summary> /// <param name=filenameurl>文件路径</param> /// <param name=table>d
推荐度:
导读Asp.net中把Excel数据存储至SQL Server中的具体实现方法: ExcelWrapper 代码如下: /// <summary> /// 查询EXCEL电子表格添加到DATASET /// </summary> /// <param name=filenameurl>文件路径</param> /// <param name=table>d

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 = "程序出错,请您检查需要导入的表!";
            }
        }

效果图

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

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

Asp.net中把Excel数据存储至SQL Server中的具体实现方法: ExcelWrapper 代码如下: /// <summary> /// 查询EXCEL电子表格添加到DATASET /// </summary> /// <param name=filenameurl>文件路径</param> /// <param name=table>d
推荐度:
标签: 中的 方法 excel
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top