亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

如何編寫xls表格導入數據庫功能代碼

發布時間:2021-10-09 09:30:33 來源:億速云 閱讀:106 作者:iii 欄目:開發技術

這篇文章主要介紹“如何編寫xls表格導入數據庫功能代碼”,在日常操作中,相信很多人在如何編寫xls表格導入數據庫功能代碼問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何編寫xls表格導入數據庫功能代碼”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

復制代碼 代碼如下:

protected void Button1_Click(object sender, EventArgs e)
        {

            if (FileUpload1.HasFile == false)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "shifou", "<script>alert('請您選擇   Excel文件')</script> ");
                return;
            }
            string Name = FileUpload1.PostedFile.FileName; //獲取初始文件名
            int j = Name.LastIndexOf("."); //取得文件名中最后一個"."的索引
            string newext1 = Name.Substring(j); //獲取文件擴展名
            if (newext1 != ".xls" && newext1 != ".xlsx")
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "wenjian", "<script>alert('只可以選擇  Excel文件')</script>");
                return;//當選擇的不是Excel文件時,返回
            }
            SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            cn.Open();
            //savePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);//    FileUpload1.PostedFile.FileName.ToString();
            if (FileUpload1.HasFile == false)//HasFile用來檢查FileUpload是否有指定文件
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "wenjian", "<script>alert('Excel路徑有  問題')</script>");
                return;//當無文件時,返回
            }

            filename = FileUpload1.FileName;  //Path.GetFileNameWithoutExtension  (FileUpload1.PostedFile.FileName);
            savePath = Server.MapPath(("../../UploadFiles/xls/") + filename);
            FileUpload1.SaveAs(savePath); //保存XLS文件

            DataSet ds = ExecleDs(savePath, filename);          //調用自定義方法
            DataRow[] dr = ds.Tables[0].Select();            //定義一個DataRow數組
            int rowsnum = ds.Tables[0].Rows.Count;
            if (rowsnum == 0)
            {
                //當Excel表為空時,對用戶進行提示
                Page.ClientScript.RegisterStartupScript(this.GetType(), "kong", "<script>alert('Excel表為空表,   無數據!')</script>");
            }
            else
            {
                    for (int i = 0; i < dr.Length; i++)
                    {

        //自定義的model-student
                        student.belong = "";
                        student.birthday = Convert.ToDateTime("1990-1-1");
                        student.classid = 1;
                        student.id = 1;
                        student.inschool = Convert.ToDateTime("1990-1-1");
                        student.phone = "";
                        student.sex = "";
                        student.type = "";
                        student.isaudite = "否";
                        student.roleid = Convert.ToInt32("22");
                        try
                        {
                            student.stuname = dr[i]["姓名"].ToString();
                            student.stuid = dr[i]["學號"].ToString();
                            student.username = dr[i]["姓名"].ToString();
                            student.userpassword = dr[i]["學號"].ToString();
                        }
                        catch(Exception ex) {
                            Response.Write("<script>alert('"+ex.Message+"')</script>");
                            return;
                        }
                        DataSet ds2 = studentbll.GetList("stuid='" + dr[i]["學號"].ToString() + "'");
                        if (ds2.Tables[0].Rows.Count == 0)
                        {
                            try
                            {
                                bool addstudent = Convert.ToBoolean(studentbll.Add(student));
                                if (addstudent)
    Page.ClientScript.RegisterStartupScript(this.GetType(), "daochu",     "<script>alert('Excle表導入成功!')</script>");
                            }
                            catch (MembershipCreateUserException ex)       //捕捉異常
                            {
                                Page.ClientScript.RegisterStartupScript(this.GetType(), "chongfu",      "<script>alert('導入內容:" + ex.Message + "')</script> ");
                            }
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(this.GetType(), "chongfu", "<script>alert    ('內容重復!禁止導入')</script> ");
                            continue;
                        }
                    }
            }
        }

  public 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);
            OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet18$]", conn);//【sheet18$】是表格名

            DataSet ds = new DataSet();
            odda.Fill(ds, table);
            return ds;
        }

到此,關于“如何編寫xls表格導入數據庫功能代碼”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

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

AI

汾西县| 孝昌县| 彰化市| 濉溪县| 泸州市| 蕲春县| 时尚| 遵义市| 霍城县| 曲阜市| 齐齐哈尔市| 汉沽区| 仪陇县| 临湘市| 碌曲县| 邓州市| 扶风县| 襄汾县| 辽源市| 台州市| 邹平县| 万山特区| 城步| 大兴区| 吉木乃县| 漯河市| 岱山县| 乐业县| 山东省| 利津县| 顺义区| 张北县| 砚山县| 来凤县| 扎鲁特旗| 商南县| 德庆县| 灵川县| 阜新市| 那坡县| 大同市|