Aspx ">
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)