Call Excel 2000 in Visual C ++

Source: Internet
Author: User

Call Excel 2000 in Visual C ++


--------------------------------------------------------------------------------


When developing software, you often need to output the data to Excel 2000. In Excel 2000, the data is further formatted or computed. It is easy to process in Visual Basic. The vbprogramming help of Excel 2000 is described in detail. In Visual C ++, how does one handle it? The ActiveX automation function of Excel 2000 is similar to that in VB. Note the following:
The getproperty () or setproperty (newvalue) function must be used to read or assign values to an object's attribute values. The value or value assignment cannot be directly used in VB. For example: Worksheet. getcount (), Worksheet. setname ("sheet1 ").
To reference a member object in a collection object, you must use the getitem () function of the collection object. For example, worksheets. getitem (colevariant (long) 1) or worksheets. getitem (colevariant ("sheet1") gets the first worksheet.
The variant, BSTR, and safearray data types are often used in the COM interface. The variant data type is a Union that represents almost all types of data. For specific usage, see the description in msdn. The class _ variant_t is an encapsulation of the variant data type. In the vbprogramming help of Excel 2000, if a function or attribute requires a value, the Data Type of the value is usually variant. In the class definition that encapsulates the Excel 2000 object, describes the required data types. BSTR is a data structure that includes string and string length. Class _ bstr_t is the encapsulation of the BSTR data type. The string mentioned in the vbprogramming help of Excel 2000 usually refers to BSTR. For the data type of specific function parameters or attributes, see the definition of the class that encapsulates the object. Safearray is a structure that includes arrays and array boundaries. content outside the array boundary is not allowed to be accessed. The array mentioned in the VB programming help in Excel 2000 refers to safearray. For more information about how to process safearray, see msdn.
For default parameters and default values. In VB, function parameters can be blank, which is not allowed in VC ++. All parameters must be filled in completely. If you want to specify a parameter as the default value, you can specify different default values based on the parameter data type. When the parameter data type is string, a string with a length of 0 can be used. If the parameter is of the variant type, you can use the constant vtmissing, which is defined in comdef. h. You can also use _ variant_t (disp_e_paramnotfound, vt_error) to generate a variant object.
The Set object in an Excel object sometimes contains some sub-objects. For example, a Range object can be a set of cells or a set of columns or rows, range. getitem (1) can return cell, column, or row objects.
Use the idispatch class object to reference or pass objects of objects, and sometimes use variant to wrap idispatch.
The following is a source program that demonstrates how to start Excel 2000, use a template file to generate a new document, and fill in a text section in the first unit of the "sheet1" worksheet of this document, set the column width of the first column, call a macro in a template, execute a program, and print and preview the Excel document. Template File Name: mytemplate. xlt. Program in Visual C ++ 6.0 SP4, Windows 2000 Professional SP-1 debug through.
First, use visual c ++ 6.0 to create a dialog box-based MFC project, share DLL, and Win32 platform. Project name exceltest. Add a button in the main dialog box,
Id idc_exceltest
Caption test Excel
Double-click this button to add the member function void cexceltestdlg: onexceltest ().
In bool cexceltestapp: initinstance (), add the code before DLG. domodal:
If (coinitialize (null )! = 0)
{
Afxmessagebox ("failed to initialize com support library! ");
Exit (1 );
}
Before the return false; statement, add:
Couninitialize ();
Choose menu-> View-> classwizade, open the classwizade window, select Add class-> from a type library, and select D:/program files/Microsoft Office/office/excel9.olb (D: /program files/Microsoft Office/is the installation directory of Microsoft Office 2000 on the local machine, which can be modified according to the actual installation directory on the personal machine ). Select _ application, workbooks, _ workbook, worksheets, _ worksheet, and range to add new classes: _ application, workbooks, _ workbook, worksheets, _ worksheet, and range. The header file is excel9.h, source File excel9.cpp.
In the header of the exceltestdlg. cpp file, under the # include "exceltestdlg. H" Statement, add:
# Include "comdef. H"
# Include "excel9.h"
Add the following code to the void cexceltestdlg: onexceltest () function:
Void cexceltestdlg: onexceltest ()
{
_ Application excelapp;
Workbooks wbsmybooks;
_ Workbook wbmybook;
Worksheets wssmysheets;
_ Worksheet wsmysheet;
Range rgmyrge;
// Create an Excel 2000 Server (start Excel)

If (! Excelapp. createdispatch ("Excel. Application", null ))
{
Afxmessagebox ("An error occurred while creating the Excel service! ");
Exit (1 );
}
// Create a new document using the template file
Wbsmybooks. attachdispatch (excelapp. getworkbooks (), true );
Wbmybook. attachdispatch (wbsmybooks. Add (_ variant_t ("G: // exceltest // mytemplate. xlt ")));
// Obtain the worksheets
Wssmysheets. attachdispatch (wbmybook. getworksheets (), true );
// Get sheet1
Wsmysheet. attachdispatch (wssmysheets. getitem (_ variant_t ("sheet1"), true );
// Obtain all cells. rgmyrge is the set of cells.
Rgmyrge. attachdispatch (wsmysheet. getcells (), true );
// Set the unit value in one row and one column
Rgmyrge. setitem (_ variant_t (long) 1), _ variant_t (long) 1), _ variant_t ("This Is A Excel test program! "));
// Obtain all columns
Rgmyrge. attachdispatch (wsmysheet. getcolumns (), true );
// Obtain the first column
Rgmyrge. attachdispatch (rgmyrge. getitem (_ variant_t (long) 1), vtmissing). pdispval, true );
// Set the column width
Rgmyrge. setcolumnwidth (_ variant_t (long) 200 ));
// Call the macro pre-stored in the template
Excelapp. Run (_ variant_t ("copyrow"), _ variant_t (long) 10), vtmissing, vtmissing,
Vtmissing,
Vtmissing,
Vtmissing,
Vtmissing, vtmissing );
// Print and preview
Wbmybook. setsaved (true );
Excelapp. setvisible (true );
Wbmybook. printpreview (_ variant_t (false ));
// Release the object
Rgmyrge. releasedispatch ();
Wsmysheet. releasedispatch ();
Wssmysheets. releasedispatch ();
Wbmybook. releasedispatch ();
Wbsmybooks. releasedispatch ();
Excelapp. releasedispatch ();
}
After the above programs are added, you can run and view the results.

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.