Use ASP. NET to access Excel documents

Source: Internet
Author: User
Excel is a software in Microsoft's office suite. It is mainly used to process workbooks. Excel is welcomed by our office staff for its user-friendly interface and rapid data processing. Therefore, many documents are saved in Excel format. For programmers, in programming, we often need to access Excel files to obtain data. However, because Excel files are not standard databases, it is difficult to access them in programming languages.

ASP. NET is a product highly recommended by Microsoft. As an important component of the. NET Framework framework, it is mainly used for web design. The new design concept and powerful functions Make ASP. NET more and more popular. It is also the powerful functionality of ASP. NET that makes it relatively simple to access Excel documents. The following is an example to illustrate how ASP. NET accesses the Excel document.

I. program design and running environment

(1). Windows 2000 Professional

(2). Net Framework SDK beta 2

(3). Microsoft Access Data component 2.6 (madc2.6)

II. Specific design ideas

(1). Obtain the name of the Excel file to be accessed

(2) read the content of an Excel file

(3). Display in datagrid format

The following describes some key steps.

Iii. Key Steps of Program Design

(1).for convenience, we will set the excelfile to the root directory of the C drive and its name is test.xls.

(2). to read an Excel file, we must understand a namespace -- system. Data. oledb. System. Data. oledb has three objects: oledbconnection, oledbcommand, and oledbdataadapter. We use them to access Excel files.

I> the oledbconnection object mainly provides the connection method.

II> the oledbcommand object provides specific operations on the target.

III> oledbdataadapter objects are returned data sets after different operations are performed on objects.

To read the test.xls of the c drive, you can complete the following statements:

Dim myoledbconnection as oledbconnection = new
Oledbconnection ("provider = Microsoft. Jet. oledb.4.0 ;"&_
"Data Source = C: est.xls ;"&_
"Extended properties =" "Excel 8.0 ;""")
Dim myoledbcommand as oledbcommand = new oledbcommand ("select *
From [sheet1 $] ", myoledbconnection) 'if you want to read the contents of sheet2
, Change sheet1 $ to sheet2 $.
Dim mydata as oledbdataadapter = new
Oledbdataadapter (myoledbcommand)

(3) After reading the content of an Excel file, you need to display it in a DataGrid. For display, another namespace -- system. Data is also used. It has an object dataset, which can help with the DataGrid to display data in the form of a DataGrid. The dataset read at this time is not represented by the object dataset, which requires conversion. Fortunately, the oledbdataadapter object provides a method named fill to complete the conversion. The specific procedure is as follows:

Dim mydataset as new dataset ()
Mydata. Fill (mydataset)
'Convert oledbdataadapter object to dataset.
Datagrid1.datasource = mydataset. Tables (0). defaultview
Datagrid1.databind () 'completes data help and displays data

4. Specific source code:

<% @ Page Language = "VB" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. oledb" %>
<Script language = "VB" runat = "server">
Sub page_load (sender as object, e as eventargs)
Dim mydataset as new dataset ()
Dim myoledbconnection as oledbconnection = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0 ;"&_
"Data Source = C: 2.xls ;"&_
"Extended properties =" "Excel 8.0 ;""")
Dim myoledbcommand as oledbcommand = new oledbcommand ("select * from [sheet1 $]", myoledbconnection)
Dim mydata as oledbdataadapter = new oledbdataadapter (myoledbcommand)

Mydata. Fill (mydataset)

Datagrid1.datasource = mydataset. Tables (0). defaultview
Datagrid1.databind ()
End sub
</SCRIPT>

<HTML>
<Head> <Body>
<Asp: Label id = "L1" runat = "server"> Read the test.xls file under the C-root directory and display it as a DataGrid. </ASP: Label>
<Asp: DataGrid id = maid = "server"/>
</Body>
</Html>

V. Summary

So far, an ASP. the net program is complete. If your machine reaches the running environment mentioned above, you can create an ASP. net Program Web virtual directory, copy an Excel file to the C drive root directory, run the program in the browser, you can see the Excel file content.

Recorded on this site

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.