Add Namespaces First
Copy Code code as follows:
Using System.Data.OleDb;
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
strfile = request.querystring["filename"];//file path passed 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 ' ";//string to connect Excel
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 = "Upload successful, the data is as follows, please confirm:";//lblmes for label, display hint information
}
catch (OleDbException)
{
string filename = filepath. Substring (filepath. LastIndexOf ('/') + 1);
Lblmes. Text = "Error!" Please make sure the upload file is correct! The currently uploaded file is: "+ filename;"
Lbtnsure.visible = false;
}
catch (Exception ee)
{
Lblmes. Text = ee. message;
}
}
asp.net read and display the implementation code for Excel data
Building dynamic Excel with C # in asp.net
asp.net to manipulate Excel implementation code