ASP. NET import data into database from Excel table

Source: Internet
Author: User

Http://www.cnblogs.com/hfzsjz/archive/2010/12/31/1922901.html

Http://hi.baidu.com/ctguyg/item/ebc857e90e436ae1fb42ba01

1   <formAction=""Method= "POST"runat= "Server">2         <Div>3       <span>Please select a file:</span><Asp:fileuploadID= "FileUpload1"runat= "Server" />4         <Asp:buttonID= "Btnexport"runat= "Server"Text= "Import"onclick= "Btnexport_click" />5     </Div>6     <Div>7         <Asp:gridviewID= "GridView1"runat= "Server">8         </Asp:gridview>9     </Div>Ten     </form>
1         protected voidBtnexport_click (Objectsender, EventArgs e)2         {3             Try4             {5                 if( This. Fileupload1.hasfile)6                 {7DataTable INPUTDT =NewDataTable ();8                     intLen = This. FileUpload1.FileName.ToString (). Trim (). Length;9                     stringPath ="~/temp/upfile/"+ This. FileUpload1.FileName.ToString (). Trim ();TenPath =Server.MapPath (path); One                      This. Fileupload1.saveas (path);//Uploading Files AINPUTDT = Jdbms. DBUtility.MDBHelper.InputExcel (Path, This. FileUpload1.FileName.ToString (). Trim (). Substring (0, Len-4)); -                     if(session["Inputdt"] !=NULL) -Session.remove ("Inputdt"); theSession.add ("Inputdt", INPUTDT); -                     if(Inputdt. Rows.Count >0) -                     { -                          This. Gridview1.datasource =Inputdt; +                          This. Gridview1.databind (); -                     } +                 } A                 Else at                 { -                     Throw NewException ("Please select the path of the import table"); -                 } -             } -             Catch(Exception ex) -             { inResponse.Write ("<script language= ' javascript ' >alert ('"+ ex. Message +"');</script>"); -             } to}
1      /// <summary>2         ///import data into a data set3         /// </summary>4         /// <param name= "Path" ></param>5         /// <param name= "TableName" ></param>6         /// <param name= "tablename2" >If this has a name for him, no words to TableName.</param>7         /// <returns></returns>8          Public StaticDataTable Inputexcel (stringPath,stringTableName)9         {Ten             Try One             { A                 stringstrconn ="provider=microsoft.jet.oledb.4.0;"+"Data source="+ Path +";"+"Extended Properties=excel 8.0;"; -OleDbConnection conn =NewOleDbConnection (strconn); - Conn. Open (); the                 stringStrexcel =""; -OleDbDataAdapter mycommand =NULL; -Strexcel ="select * FROM ["+ TableName +"$]"; -MyCommand =NewOleDbDataAdapter (Strexcel, strconn); +DataTable dt =NewDataTable (); - mycommand.fill (DT); + Conn. Close (); A                 returnDT; at             } -             Catch(Exception ex) -             { -                 Throw NewException (ex. Message); -             } -}

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.