VS2008 in C + + open Excel (MFC)

Source: Internet
Author: User

VS2008 in C + + Open Excel ( MFC )--excerpt from the Web and refined

Step One: Establish project (new project)

English version

Chinese version

Select MFC Application (dialog-based project) under C + +

English version

Chinese version

Select dialog based (based on dialog), Chinese. Then click Finish (Finish)

Step Two: import the Excel header file

English version

Chinese version-New class

Chinese version-MFC class in New TypeLib

Chinese version-Import Excel related classes

After selecting Project->add Class->mfc class from Typelib, click Add to enter the MFC Class from Typelib Wizard dialog box,

Select Microsoft Excel 11.0 Object library<1.5> (I'm using Microsoft Office2003) to add the middle class in. Click Finish

and start adding in TExcel.cpp:

#include "CApplication.h"

#include "CRange.h"

#include "CSheets.h"

#include "CWorkbook.h"

#include "CWorkbooks.h"

#include "CWorksheet.h"

#include "CWorksheets.h"

In each of the added header files, remove

#import "D:\\Program Files x64\\microsoft office\\office15\\excel. EXE "No_namespace

Step three: open COM port

Add AfxOleInit ();

Fourth Step: Build a button

Select the button in Toolbox to click in the Texcel.rc idd_texcel_dialog to create a button that will change the caption in the property to "open Excel"

Fifth Step: writing a program for button buttons

Double-click "Open Excel"

1 voidCtexceldlg::onbnclickedbutton1 ()2 {3     //TODO: Add control notification handler code here4CApplication objapp;//determine if an Excel application exists5 Cworkbooks objbooks; 6 CWorkbook Objbook;7 CWorksheets objsheets;8 Cworksheet objsheet;9CRange Objrange,usedrange;//Cell GetTen VARIANT ret; One  AObjapp.createdispatch (_t ("Excel.Application"));//determine if an Excel application exists -COleVariant voptional ((Long) disp_e_paramnotfound,vt_error); - objapp.put_visible (TRUE); the Objapp.put_usercontrol (TRUE); -Lpdispatch pwkbooks=objapp.get_workbooks (); - Objbooks.attachdispatch (pwkbooks,true); -     CharCurrentprogrampath[max_path]; +objbooks=objapp.get_workbooks (); -objbook=objBooks.Add (voptional); +objsheets=objbook.get_sheets (); A CString str; at  -Str="A1"; -Objsheet=objsheets.get_item (COleVariant ( Short)1)); -Objrange=Objsheet.get_range (COleVariant (str), COleVariant (str)); -Objrange=objsheet.get_range (COleVariant (_t ("C4")), COleVariant (_t ("B4"))); -Objrange.put_formula (COleVariant (_t ("=rand () *100000"))); inObjrange.put_numberformat (COleVariant (_t ("$0.00"))); -Usedrange=Objrange.get_entirecolumn (); to Usedrange.autofit (); +}
View Code

Sixth step: Avoid error 2 error C2059:syntax error: ', '

Change CRange.h under Variant DialogBox () to Variant _DialogBox ()

Last run:

VS2008 in C + + open Excel (MFC)

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.