ASP.net connection Excel Code

Source: Internet
Author: User

First, add a namespace.
Copy codeThe 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.