1DataTable dtgbpatient =NewDataTable ();2 3 stringstrconn;stringExcelname;4 5 //Note: Consider an Excel file as a database, a sheet as a table. Syntax "SELECT * FROM [sheet1$]", forms to use "[]" and "$"6 7 //1, HDR indicates to be the first row as data or column name, as the data with Hdr=no, as the column name with Hdr=yes;8 //2, through the imex=1 to the mixed type as text-based reading, to avoid null values. 9strconn =@"Provider=Microsoft.Jet.OLEDB.4.0;Data source= ' {0} '; Extended properties= ' Excel 8.0; Hdr=yes;imex=1 ';";Ten stringstrconnection =string. Format (strconn, excelpath); OneOleDbConnection conn =NewOleDbConnection (strconnection); A Conn. Open (); -OleDbDataAdapter Oada =NewOleDbDataAdapter ("select * FROM ["+ Excelname +"$]", strconnection); - theDtgbpatient.tablename ="Gbpatientinfo"; -Oada. Fill (dtgbpatient);//Get a DataTable -Conn. Close ();
Transferred from: http://blog.sina.com.cn/s/blog_62d1205301010llt.html
C # reads Excel data into a DataTable