Asp.net|datagrid|excel
First, add the following reference to the header of the *.aspx.cs file:
Using system.data.oledb;//is used to bind an Excel file to a 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 ();
}
Description: Bang.xls is an Excel file that needs to be tied to a DataGrid (Dgbang).
Sheet1 is a work form in Bang.xls (work sheet)
"Hdr=yes": Indicates that the first row contains column names, not data
"Imex=1;": Tell Drive always read cross data column as text
("Hdr=yes;" indicates that the "
the" "The" the "the" "contains columnnames, not data
"Imex=1;" tells the driver to always read "intermixed" data columns as text)