Excel programming in Visual C #

Source: Internet
Author: User

Excel is a software in Microsoft Office Automation suite, which is mainly used to process spreadsheets. Excel is popular with many users for its powerful and user-friendly interface. In the office, it is because of Excel so many advantages, many important data, often in the form of Excel spreadsheet storage. This poses a problem for programmers, although Excel is powerful, but not a database, it is much easier to process data in a program than it does with data in Excel tables. So how do you read data in an Excel table with Visual C #? In the previous use of Delphi programming, for different users, they are not the same print requirements, if you want to make the application of the printing function for each user, you can imagine the program design is very complex. Then think of Excel, because the Excel table is powerful, and because almost every machine installed it, if the results of the program processing into the Excel table, so that each user can according to their needs in Excel customization of their own printing. This not only makes the program design simple, but also meet the requirements of many users, more practical. So how do you call Excel with Visual C #, and then how do you store the data in an Excel table? This article will discuss the solution to the above problems.

A Programming and operating environment

(1). Microsoft Windows 2000 Server Edition

(2). Net Framework SDK Beta 2

(3). Microsoft Data Access Component version 2.6 (MDAC2.6)

(4). Office 2000 Suite

Two Visual C # reads data from an Excel table:

This section will introduce Visual C # to read the data in Excel tables and display the data in a DataGrid as a program.

(1). How to read data:

Actually reading the data in Excel tables and reading the data in the database is very similar, because in a way Excel tables can be viewed as a single sheet of data. The main difference is that the data engine used is different. In the program in this article, the following code is used to read Excel tabular data, as follows:

//创建一个数据链接
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
file://打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
file://得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
file://关闭此数据链接
myConn.Close ( ) ;

How to read the data in an Excel table does not actually differ from reading the data in the database.

Note: Read here is the "Sample.xls" file in the C-packing directory.

(2). Display the resulting dataset with a DataGrid:

After you get the DataSet object, you can display the dataset in a DataGrid by using only the following two lines of code:

DataGrid1.DataMember= "[Sheet1$]" ;
DataGrid1.DataSource = myDataSet ;

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.