Use the Microsoft-supplied COM component to read Excel
First, read the Excel file COM class related environment to build:
1, first create a new MFC dialog box project, can be named Readwriteexcel
2. In the Add Class option,
Select MFC from Typelib, such as:
3, then find the corresponding Office version of the Excel.exe file directory, and open Excel.exe,
C:\Program Files\Microsoft Office\office15\excel. Exe
4, add 6 interfaces interface to the right of the generated class generated classes,
5, finish after completion. Include the relevant header file in the diagram at the top of the code that needs to be added to read the Excel file.
Put the contents of these 6 files: #import "C:\\Program Files\\Microsoft Office\\office15\\excel. EXE "No_namespace are commented out. Leave this section of the Applicaiton.
Second, use the existing classes and functions to read the Excel file
1. Define the following 6 classes.
CApplication Excelapp;
Cworkbooks Books;
CWorkbook Book;
CWorksheets sheets;
Cworksheet sheet;
CRange Range;
2. Create an Excel server,
Excelapp.createdispatch (_t ("Excel.Application")
Use the Excelapp.get_version () function to determine the version of Office that is installed natively
3, get the workbook container books. AttachDispatch (Excelapp.get_workbooks ()), followed by books. Open (...) function to open the selected file, which requires a file path such as CString Bookpath.
4. Then through the range.get_value2 () function, the read Excel file is converted to variant type, and finally the data is read out by setting the loop.
PS: This article is only for individuals to learn some of the content of other blog essays, many of which are using the results of others. This article is for personal use only for future reference.
The attachment will have the source code, the inside has the more detailed thought explanation. There is also code written by the Excel file. Welcome the Great God of the road to criticize.
Link: https://pan.baidu.com/s/1pLDdKxl Password: 8CHV
Find out more about C + + reading Excel form data on the Internet