C # use SQL statements to read Excel file data

Source: Internet
Author: User

1. Purpose: To read a large amount of data in an Excel file and display it on the page:

Protected void page_load (Object sender, eventargs E)
{
// Address of the Excel file
String xlspath = server. mappath ("app_data/aa.xls ");
// Link character
String connstr = "provider = Microsoft. Jet. oledb.4.0;" + "extended properties = Excel 8.0;" + "Data Source =" + xlspath;
// Query statement. sheet1 specifies the sheet.
String SQL = "select * from [sheet1 $ A1: m30]";
Dataset DS = new dataset ();

// Query data
Oledbdataadapter da = new oledbdataadapter (SQL, connstr );

// Fill in Dataset
Da. Fill (DS );
// Output, bind data
Gridview1.datasource = Ds. Tables [0];
Gridview1.databind ();
}

Note:

The valid Excel version used for the extended properties value.
For Microsoft Excel 8.0 ( 97 ), 9.0 ( 2000 ) And 10.0 ( 2002 ) Workbook, use Excel 8.0 .
For Microsoft Excel 5.0 And 7.0 ( 95 ) Workbook, use Excel 5.0 .
For Microsoft Excel 4.0 Workbook, use Excel 4.0 .
For Microsoft Excel 3.0 Workbook, use Excel 3.0 .

This article reference: Xiaofeng residual month blog, URL: http://zt.cnblogs.com/page/42229/

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.