asp.net to connect Excel code _ Practical Tips

Source: Internet
Author: User
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

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.