First, add the following reference in the *. aspx. CS file header:
Using system. Data. oledb; // used to bind an Excel file to the DataGrid
Second, add the following sample code to the page_load () function:
If (! Ispostback)
{
String strcon = "provider = Microsoft. jet. oledb.4.0; Data Source = "+ server. mappath (".. /xls_bang/bang.xls ") +"; extended properties = 'excel 8.0; HDR = yes; IMEX = 1 '";
Oledbconnection olecon = new oledbconnection (strcon );
Oledbdataadapter oleda = new oledbdataadapter ("select * from [sheet1 $]", olecon );
Dataset DS = new dataset ();
Oleda. Fill (DS );
Dgbang. datasource = Ds;
Dgbang. databind ();
}
Note: bang.xls is an Excel file that needs to be bound to the DataGrid (dgbang.
Sheet1is a work sheet in bang.xls)
"HDR = yes;": indicates that the first line contains the column name, not the data.
"IMEX = 1;": indicates that the driver always reads cross data columns as text
("HDR = yes;" indicates that the first row contains columnnames, not data
"IMEX = 1;" tells the driver to always read "intermixed" Data columns as text)