Asp.net reads Excel Data and fills in DataSet and GridView code

Source: Internet
Author: User

Asp tutorial. net reads excel Data and fills in dataset and gridview code
// Connection string

String ph = string. format ("upload \ {0}", name );
String xlspath = server. mappath (ph );
String connstr = "provider = microsoft. jet. oledb.4.0; "+" extended properties = "excel 8.0; hdr = no; imex = 1"; "+" data source = "+ xlspath;
String SQL _f = "select * from [{0}]";

Oledbconnection conn = null;
Oledbdataadapter da = null;
Datatable tblschema = null;
Ilist <string> tblnames = null;

// Initialize the connection and open it
Conn = new oledbconnection (connstr );
Conn. open ();


// Method 2

Connection string

Dim xlspath as string = server. mappath ("~ /App_data/excelfile.xls ") 'absolute physical path
Dim connstr as string = "provider = microsoft. jet. oledb.4.0; data source =" & xlspath & "; extended properties = excel 8.0 ;"
'Query statement
Dim SQL as string = "select * from [sheet1 $]"

Dim ds as dataset = new dataset ()
Dim da as oledb. oledbdataadapter = new oledb. oledbdataadapter (SQL, connstr)
Da. fill (ds) 'fill dataset

'Here we operate the data in dataset

'Output, bind data
Gridview1.datasource = ds. tables (0)
Gridview1.databind ()

The code above demonstrates the code bound to the gridview.

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.