You can use OLE DB (Object linking and embeding DataBase) to access excel in. Net
1 usingSystem;2 usingSystem.Data;3 usingSystem.Windows.Forms;4 usingSystem.Data.OleDb;5 6 namespaceExceldemo7 {8 Public Partial classForm1:form9 {Ten PublicForm1 () One { A InitializeComponent (); - } - the Private voidButton1_Click (Objectsender, EventArgs e) - { - //xls is the excel2003-2007 version of the Excel file suffix, and xlsx is a suffix of 2007 or more versions - //var dt = exceltodatatable (@ "E:\WorkSpace\VS\ExcelDemo\test.xls", "Sheet1"); + varDT = Exceltodatatable (@"E:\WorkSpace\VS\ExcelDemo\test.xlsx","Sheet1"); - This. datagridview1.datasource=DT; + } A PrivateDataTable exceltodatatable (stringStrexcelfilename,stringstrsheetname) at { - //both Jet and Ace are interface engines that connect Excel objects, but they differ. - //string strconn = "provider=microsoft.jet.oledb.8.0;" + "Data source=" + Strexcelfilename + ";" + - //"Extended properties= ' Excel 8.0; Hdr=yes;imex=1 '; "; - stringstrconn =@"provider=microsoft.ace.oledb.12.0;"+"Data source="+ Strexcelfilename +";"+ - "Extended properties= ' Excel 12.0; Hdr=yes;imex=1 ';"; in stringStrexcel =string. Format ("SELECT * FROM [{0}$]", strsheetname); - //Create an OLE DB connection, pass in a different connection string for a specific data source type toOleDbConnection connection =NewOleDbConnection (strconn); + connection. Open (); -OleDbDataAdapter da =NewOleDbDataAdapter (strexcel,connection); the varDS =NewDataSet (); * da. Fill (ds,strsheetname); $ returnds. Tables[strsheetname];Panax Notoginseng } - } the}
The first use of the Excel object interface engine was jet, but during testing it was found that for the Excel2013 version of the file, the error "External table is not a standard format" when executing to 31. Save Excel as a 2003-2007 version without an error.
The original jet engine and Ace engine are different, specific reference http://blog.csdn.net/xifeijian/article/details/8333396
. NET, using OLE DB to read Excel