ASP. NET Excel Data Import Database---2

Source: Internet
Author: User
Tags import database

protected voidBtnimport_click (Objectsender, EventArgs e) {        if(Fileupload1.hasfile = =false)//HasFile used to check if FileUpload has a specified file{Response.Write ("<script>alert (' Please select Excel file ') </script>"); return;//when there is no file, return        }        stringIsxls = System.IO.Path.GetExtension (fileupload1.filename). ToString (). ToLower ();//System.IO.Path.GetExtension getting the file name extension        if(Isxls! =". xls") {Response.Write ("<script>alert (' Can only select Excel Files ') </script>"); return;//When you select an Excel file that is not, return        }        stringfilename = fileupload1.filename;//get execle filename datetime date function        stringSavepath = Server.MapPath (("upfiles\\") + filename);//Server.MapPath obtaining a virtual server relative pathFileupload1.saveas (Savepath);//SaveAs saves uploaded file contents on the serverDataSet ds = excelsqlconnection (savepath, filename);//calling a custom methoddatarow[] Dr = ds. tables[0]. Select ();//define a DataRow array        intRowsnum = ds. tables[0].        Rows.Count; if(Rowsnum = =0) {Response.Write ("<script>alert (' Excel table is empty table, no data! ') </script>");//prompt the user when the Excel table is empty        }        Else        {             for(inti =0; I < Dr. Length; i++)            {                //In front of you need to create a "upfiles" folder, the others do not have to control, you just need to get the value of Excel in the following way, and then put these values in your way to insert into the database                stringtitle = dr[i]["title"].                ToString (); stringLinkurl = dr[i]["Link Address"].                ToString (); stringCategoryName = dr[i]["category"].                ToString (); stringCustomerName = dr[i]["content providers"].                                ToString (); //Response.Write ("<script>alert" (' Import content: ' + ex. Message + "') </script>");} Response.Write ("<script>alert (' excle table import succeeded! '); </script>"); }    }    #regionConnect Excel to read Excel data and return DataSet data collection/// <summary>    ///connect Excel to read Excel data and return DataSet data collection/// </summary>    /// <param name= "filepath" >Excel Server Path</param>    /// <param name= "TableName" >Excel table name</param>    /// <returns></returns>     Public StaticSystem.Data.DataSet Excelsqlconnection (stringFilePathstringtableName) {        stringStrcon ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ filepath +"; Extended properties= ' Excel 8.0; Hdr=yes;imex=1 '"; OleDbConnection Excelconn=NewOleDbConnection (Strcon); Try        {            stringstrcom =string. Format ("SELECT * FROM [sheet1$]");            Excelconn.open (); OleDbDataAdapter mycommand=NewOleDbDataAdapter (strcom, excelconn); DataSet DS=NewDataSet (); Mycommand.fill (DS,"["+ TableName +"$]");            Excelconn.close (); returnds; }        Catch{excelconn.close (); return NULL; }    }    #endregion

The Excel format is as follows:

Title Link Address Classification Content providers
parody of Japanese woman 1 Http://fegnhuang.com Photo Phoenix Video
parody of Japanese woman 2 Http://fegnhuang.com Photo Phoenix Video
Parody of Japanese woman 3 Http://fegnhuang.com Funny Phoenix Video
Parody of Japanese woman 4 Http://fegnhuang.com Funny Phoenix Video
Parody of Japanese woman 5 Http://fegnhuang.com Funny Mango TV
Parody of Japanese woman 6 Http://fegnhuang.com Beauty Mango TV
Parody of Japanese woman 7 Http://fegnhuang.com Beauty Mango TV
Parody of Japanese woman 8 Http://fegnhuang.com Beauty Mango TV
Parody of Japanese woman 9 Http://fegnhuang.com Fashion Sina Video
Parody of Japanese woman 10 Http://fegnhuang.com Fashion Sina Video

ASP. NET Excel Data Import Database---2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.