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); - } -}