欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net中把Excel数据存储至SQL Server中

程序员文章站 2022-06-25 15:52:50
操作图     excelwrapper         /// <...

操作图

 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中





摘自 it胖子的专栏