to bind Excel files (. xls) to datagrid!

Source: Internet
Author: User
Tags bind first row|datagrid|excel

First, add the following reference to the header of the *.aspx.cs file:

Using;//is used to bind an Excel file to a DataGrid

Second, add the following sample code to the Page_Load () function:

if (! IsPostBack)
String strcon= "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +server.mappath (".. /xls_bang/bang.xls ") +"; Extended properties= ' Excel 8.0; Hdr=yes;imex=1 ' ";
OleDbConnection olecon=new OleDbConnection (Strcon);
OleDbDataAdapter oleda=new OleDbDataAdapter ("select * from [sheet1$]", Olecon);
DataSet ds=new DataSet ();
Oleda.fill (DS);
Dgbang.databind ();


Description: Bang.xls is an Excel file that needs to be tied to a DataGrid (Dgbang).

Sheet1 is a work form in Bang.xls (work sheet)

"Hdr=yes": Indicates that the first row contains column names, not data

"Imex=1;": Tell Drive always read cross data column as text

("Hdr=yes;" indicates that the "
the" "The" the "the" "contains columnnames, not data
"Imex=1;" tells the driver to always read "intermixed" data columns as text)

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: 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.