First, add a namespace.
CopyCode The Code is as follows: using system. Data. oledb;
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Strfile = request. querystring ["FILENAME"]; // path of the File Uploaded from other pages
Excel (strfile );
}
}
Private void Excel (string filepath)
{
Try
{
Dataset DS = new dataset ();
String connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + filepath +
"; Extended properties = 'excel 8.0; HDR = yes; IMEX = 1'"; // connect the Excel String
String query = "select * from [STUDENT $]"; // table name in Excel
Oledbcommand olecommand = new oledbcommand (query, new oledbconnection (connstr ));
Oledbdataadapter oleadapter = new oledbdataadapter (olecommand );
Oleadapter. Fill (DS, "[STUDENT $]");
Rowcount = Ds. Tables [0]. Rows. count;
Gridview1.datasource = Ds;
Gridview1.databind ();
Lblmes. Text = "uploaded successfully. The data is as follows. Please confirm:"; // The lblmes is a label and a prompt is displayed.
}
Catch (oledbexception)
{
String filename = filepath. substring (filepath. lastindexof ('/') + 1 );
Lblmes. Text = "error! Check whether the uploaded file is correct! The currently uploaded file is: "+ filename;
Lbtnsure. Visible = false;
}
Catch (exception ee)
{
Lblmes. Text = ee. message;
}
}
Implementation Code for Asp.net to read and display Excel Data
Create dynamic excel with C # In Asp.net
Implementation Code of Asp.net Excel operations