Visual c ++ Excel programming section

Source: Internet
Author: User

I recently studied the Excel programming related content because it helps the Youth League committee proofread data. The data to be proofread may be incorrectly entered manually, such as the student ID or name. The correct data is an Excel file exported from the database. The final steps are as follows:

There are roughly three ways to read and write Excel files:

1. database connection, ODBC/ADO.

The ODBC driver of XLS must be installed in use. Generally, it is installed with Excel. However, there are exceptions. If you are using a 64-bit system, you will find that the XLS driver does not exist. This is because there is no XLS 64-bit driver, and I am using a 64-bit windows, finally, you can test the program completed on the Virtual Machine (XP. When writing, we found that the process of Excel is still running in the background using ADO, which means the second method is the most fundamental.

Ado programming model (the ado2.5 version also adds stream objects to process big data; later, there is ADOX, an extended version)

2. COM automation, or interoperability (C ++ com InterOP). Therefore, you must understand the EXCEL object model.

The most common application/workbook/worksheet/range classes can be used to implement the vast majority of operations using their attributes and methods. Many of these materials are based on VBA. When converting to C ++ code, you must convert the com data type to the corresponding data type (for example, use the _ variant_t and _ bstr_t types to wrap the Variant Structure and BSTR pointer under com ). ADO and Excel object models are both COM components. Microsoft provides smart pointer templates such as _ connectionptr/_ recordsetptr/_ applicationptr /, you can avoid complex operations such as reference counting and release when using COM.

3. Directly analyze the XLS format. This is the method with the lowest dependence. It is suitable for Excel files with no complex data and charts, and does not require an Excel or database driver. There are two articles on codeproject: miniexcel and basic_excel. There are also third-party controls or libraries, most of which are commercial versions, such as the Excel read and write controls for tables in China and libxl (powerful ).

Some feelings are: programming under Windows will certainly deal with COM, so we should pay attention to it, and there is the MFC framework. Although familiar with it, it is indeed a little heavy, in many cases, it is obvious that bad code tastes like long methods, repeated code, and resource files should be separated from code ..., Even if it is separated, the reusability is not high. It seems that we have to work hard to improve it. wtl/ATL is a good choice. Interoperability is still promising. Interaction between C ++/COM/. NET is exciting.

 

A few good references:

Http://blog.csdn.net/bodybo/archive/2009/05/27/4220561.aspx

Use smart pointer to Operate Excel in VC

Http://msdn.microsoft.com/zh-cn/library/wxks80sb.aspx

How to: sort the data in a worksheet programmatically

Http://support.microsoft.com/kb/257819/zh-cn#RetrieveExcel

How to use ADO in Visual Basic or VBA to process Excel Data

Http://conkeyn.javaeye.com.sixxs.org/blog/229992

Conversion of VC ++ Data Types

Http://www.cnblogs.com/sensizhe/archive/2009/04/01/1427362.html

VC uses ADO to Operate Excel

In addition, several good articles in codeproject and codeguru will not be listed.

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.