. NET, using OLE DB to read Excel

Source: Internet
Author: User
Tags ole

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

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.