Reads the contents of an Excel file on the Web

Source: Internet
Author: User

Click the event code. CS

    protected voidButton1_Click (Objectsender, EventArgs e) {        stringstrpath ="D:/test.xls"; stringMyString ="provider=microsoft.ace.oledb.12.0;data Source = '"+ strpath +"'; Extended properties= ' Excel 8.0; hdr=yes;imex=1; '"; //"Provider = microsoft.jet.oledb.4.0; Data Source = ' "+ strpath +"; Extended properties=excel 8.0 ";OleDbConnection Cnnxls =NewOleDbConnection (mystring); OleDbDataAdapter Myda=NewOleDbDataAdapter ("select * FROM [sheet1$]", Cnnxls); DataSet MyDs=NewDataSet ();        Myda.fill (MyDs); DataGrid1.DataSource= myds.tables[0];     Datagrid1.databind (); }

If you use the classic "Provider = microsoft.jet.oledb.4.0; Data Source = ' "+ strpath +"; Extended Properties=excel 8.0 "Error: external table is not the expected format

This is because: Microsoft.Jet.OLEDB.4.0 is the Microsoft Jet engine, which applies to version 2003 (I didn't pretend before 2003, so I don't know which version to fit down), and in 2007, Microsoft has access to Excel The primary file format is modified and renamed as. ACCDB (Access 2007 database files) and. xlsx (Excel 2007 files), so it is not supported by the Microsoft Jet engine, but Microsoft also quickly presents Microsoft Office 2 007 Desktop Drivers:data Connectivity components to support.

The workaround is to change the data provider in the connection string to microsoft.ace.oledb.12.0

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.