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 .