Using the Excel file as a data source of DataGrid is very simple, once the data is loaded, you can save the data to SQL Server or XML. We only need to simply use Ole DB Provider to access the Excel file, and then return to DataSet. Below is the Excel Data Contact.xls to display:
Name
gender
address
NET_LOVERMALEAMXH @ 21cn.com @ 21cn.com Mencius E Chapter MaleAmxh@21cn.com
Just specify an Excel path and select a worksheet with [].
The full code is as follows:
<% @ Page language = "c #" debug = "true"%>
<% @ Import namespace = "system.data"%>
<% @ Import namespace = "system.data.oledb"%>
Private Dataset CreateDataSource () {
String strconn;
StrConn = "provider = microsoft.jet.Oledb.4.0;"
"Data Source = C: //inetpub//wwwroot//contacts.xls;"
"Extended Properties = Excel 8.0;";
OLEDBCONNECTION CONN = New OLEDBCONNECTION (STRCONN);
OLEDBDataAdapter mycommand = new oledbdataadapter ("Select * from [contactList $], strconn);
Dataset mydataset = new dataset ();
Mycommand.fill (MyDataSet);
Return mydataset;
}
Public void Page_load (Object Sender, Eventargs E) {
IF (! ispostback) {
MyGrid.datasource = createdataSource ();
MyGrid.databind ();
}
}
script>