Asp. NET implementation to read Excel content and display on the Web _ practical Tips

Source: Internet
Author: User

This article describes the ASP.net implementation of reading Excel content and display on the Web, is a very practical function, share for everyone to reference. The implementation methods are as follows:

Click the event code. CS code is as follows:

protected void Button1_Click (object sender, EventArgs e)
{
    string strpath = "D:/test.xls";
    String mystring = "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 = new OleDbConnection (mystring);
    OleDbDataAdapter Myda = new OleDbDataAdapter ("SELECT * from [sheet1$]", Cnnxls);
    DataSet myds = new DataSet ();
    Myda.fill (myds);
    DataGrid1.DataSource = myds.tables[0];
    Datagrid1.databind (); 
}

Attention:

If the classic "Provider = Microsoft.Jet.OLEDB.4.0" is used; Data Source = ' + strpath + '; Extended Properties=excel 8.0 "will error: External table is not expected format

This is because: Microsoft.Jet.OLEDB.4.0 is the Microsoft Jet engine, which applies to version 2003 (which was not tested before 2003, and so does not know which version to adapt to), and in 2007, Microsoft's Access and The main file format for Excel is modified and renamed to. ACCDB (Access 2007 database files) and. xlsx (Excel 2007 files), which is not supported by the Microsoft Jet engine, but Microsoft also quickly presented Microsoft's Fice 2007 Desktop Drivers:data connectivity components to support.

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

Related Article

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.