First write, please advise!! Words not more say go directly code!
1 /// <summary>2 ///Open File3 /// </summary>4 /// <param name= "Sender" ></param>5 /// <param name= "E" ></param>6 Private voidBtn_file_click (Objectsender, EventArgs e)7 {8OpenFileDialog OpenFile =NewOpenFileDialog ();9Openfile.filter ="execl (*.xls) |*.xls|accdb (*.accdb) |*.accdb";Ten if(Openfile.showdialog () = =DialogResult.OK) { One Selectinfo (openfile.filename); A } -}
1 /// <summary>2 ///reading Data3 /// </summary>4 /// <param name= "path" ></param>5 Public voidSelectinfo (stringpath) {6 stringSuffix =path.getextension (Path);7 //string tabname = "Student";8 stringTabName ="";//the table name of the query suggests adding a TextBox control to the form9 if(Tabname.equals (""))Ten { OneTabName + ="[sheet1$]"; A } - stringstrconn =string. Empty; - //path = environment.currentdirectory; the Switch(Suffix) - { - Case ". xls": -strconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ Path +"; Extended properties= ' Excel 8.0; hdr=no;imex=1; '"; + //tabname + = "$"; - Break; + Case ". xlsx": Astrconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ Path +"; Extended properties= ' Excel 8.0; Hdr=false;imex=1 '"; at //tabname + = "$"; - Break; - Case ". mdb": -strconn ="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+path; - Break; - Case ". accdb"://Access2007 instrconn ="Provider=microsoft.ace.oledb.12.0;data source="+path; - Break; to default: + Break; - } theOleDbConnection oledbconn =NewOleDbConnection (strconn); * Try $ {Panax Notoginseng //Oledbconn.open (); - stringsql ="Select * from"+tabname; theOleDbDataAdapter Oledbda =NewOleDbDataAdapter (SQL, oledbconn); +DataSet ds =NewDataSet (); AOledbda.fill (DS,"Information"); the This. Dgv_info.datasource = ds. tables["Information"]; + } - Catch(Exception) $ { $ - Throw; - } the finally { - oledbconn.close ();Wuyi } the}
Operating effect:
Suitable for beginners. First write, God do not spit groove ah ....