MFC's interaction with Excel files

Source: Internet
Author: User

The holiday is coming to an end, with a heavy heart. Write a summary of the previous interaction between MFC and Excel files. Because the version of VS may be different in operation, so it is pointed out that the engineering environment of this article is VS2013, and we recommend that you use the latest.

When it comes to Excel, the program should not be a new requirement, there are several ways, such as the use of Office-provided interfaces, open source code, and so on. After checking some information, this paper chooses a kind of counter-up. Its implementation of the method is basically completely copied from this article, if you see this article is not clear can go to see.

    reference article says, " Pale there's no place to go, it costs a damn. "but in the effort of the bloggers, Posterity needs to do is to look at, copy a copy on the finished, The power of open source is great. demo project to provide download, if the people do not bother to do these jobs only need to download the project can be directly used up (pro-test can), the project is given in the last side.

Original address

Let's start with the main content

First step: Create an empty MFC dialog Box project

Second step: drop-down vs menu bar "Project" Item, select Add Class. In the pop-up dialog box, select MFC class in TypeLib, and click Add to bring up the Add Class from Type Library Wizard dialog box.

The Third Step: the dialog box that pops up has two options in Add Class from, if you select Registry, select Microsoft Excel 15.0 Object library<1.8> in the Available type libraries ( There may be a slight difference between Office versions, which is Office2013. If you selected file, location selects EXCEL.EXE under the Office installation path, the default installation for my 32-bit office, with the path C:\Program Files (x86) \microsoft Office\ Office15\excel. EXE ". The above two options to achieve the same effect, but the path is different, finish the effect as follows:


Fourth step: Select "Interface" in "_application", "_worksheet", "_workbook", "Worksheets", "WorkBooks", "Range" these items, put to the right, click Finish, such as:


    Fifth step: After the fourth step, the project will be more than a few import class header file " capplication.h "," Cworksheet.h" , "Cworkbook.h" , "Cworksheets.h" , "Cworkbooks.h" , "Crange.h", will each header file inside the first sentence code "#import" C:\\Program Files (x86) \\Microsoft Office\\office15\\excel. EXE "No_namespace" (the path may be different) are commented out. such as:


Sixth step: Since there is a header file, it is natural to include these header files, in the need to call the file header to write the following code:

#include "CApplication.h" #include "CRange.h" #include "CWorkbook.h" #include "CWorkbooks.h" #include "CWorksheet.h" # Include "CWorksheets.h"
    Seventh Step: This time is arguably the environment is set up, but click Compile or will report a grammatical error, fortunately, the blog mentioned above also gives a solution, positioning to "crange,h", will
Variant DialogBox () {Variant result;invokehelper (0xf5, Dispatch_method, Vt_variant, (void*) &result, NULL); return Result;}
the DialogBox in is changed to _DialogBox, now that the compilation is no problem, the next call can be, the calling code is as follows:

CApplication app; Cworkbooks Books; CWorkbook Book; CWorksheets sheets; Cworksheet sheet; CRange Range; Lpdispatch Lpdisp; COleVariant vresult; COleVariant covoptional ((Long) disp_e_paramnotfound, vt_error); if (!app. CreateDispatch (L "Excel.Application")) {AfxMessageBox (L "Cannot start Excel server!"); return;} Books. AttachDispatch (App.get_workbooks ()); CString path = L "demo.xlsx";//file path TCHAR szpath[max_path];//get current execution path GetModuleFileName (NULL, szpath, MAX_PATH); CString PathName (szpath); Pathname=pathname.left (Pathname.reversefind (_t (' \ \)) + 1); lpdisp = books. Open (Pathname+path, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, CovOptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional, covoptional); AttachDispatch (LPDISP);//Get workbook sheets. AttachDispatch (Book.get_worksheets ());//Get Worksheets Lpdisp = Book.get_activesheet ();//Get current active sheet, if a cell is in the edit state , this operation cannot be returned and will wait for sheet. AttachDispatch (LPDISP);//reads the value of the row rows, the cols column cells/* If traversal executes this code repeatedly and determines the bounds */int rows =1, cols = 2;range. AttachDispatch (Sheet.get_cells ()); Range. AttachDispatch (Range.get_item (COleVariant ((long) rows), COleVariant ((long) cols). pdispval); vresult = range.get_ Value2 (); CString data;if (vresult.vt = = VT_BSTR)//String {data = Vresult.bstrval;} else if (vresult.vt = = vt_r8)//8 bytes of the number {data. Format (L "%.0f", Vresult.dblval);} else if (vresult.vt==vt_empty)//cell Empty {data = "";} The MessageBox (data);/* If the traversal executes this code repeatedly and determines the bounds *///the object books. Close (); app. Quit ();//Exit range. ReleaseDispatch (); sheet. ReleaseDispatch (); sheets. ReleaseDispatch (); ReleaseDispatch (); books. ReleaseDispatch (); app. ReleaseDispatch ();

OK, to this realization of the Excel data read, as to create and write the same, later useful to add up again.

Project Download (VS2013)



 

MFC's interaction with Excel files

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.