Read data from an Excel file into the DataGrid

Source: Internet
Author: User
Using an Excel file as the data source of the DataGrid is very simple. Once the data is loaded in, you can save the data to SQL Server or XML. We only need to simply use ole db provider to access the Excel file and then return dataset.

<% @ Page Language = "C #" DEBUG = "true" %> <% @ import namespace = "system. data "%> <% @ import namespace =" system. data. oledb "%> <SCRIPT runat =" server "> private dataset createdatasource () {string strconn; strconn =" provider = Microsoft. jet. oledb.4.0; "+" Data Source = C :\\ Inetpub \ wwwroot \ contacts.xls; "+" extended properties = Excel 8.0; "; oledbconnection conn = new oledbconnection (strconn ); oledbdataadapter mycommand = N EW oledbdataadapter ("select * from [contactlist $]", strconn); dataset mydataset = new dataset (); mycommand. fill (mydataset); Return mydataset;} public void page_load (Object sender, eventargs e) {If (! Ispostback) {mygrid. datasource = createdatasource (); mygrid. databind () ;}</SCRIPT> <center> <form runat = "server"> <asp: dataGrid runat = "server" autogeneratecolumns = "false" width = "500" id = "mygrid"> 

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.