The C ++ programming language is widely used and can help us easily implement various functional requirements. For example, the operations on Excel tables are accepted for everyone today. Next, let's take a look at the implementation methods of C ++ Builder to Operate Excel tables. Hope to help you.
- Introduction to several different C ++ inheritance Methods
- Analysis of C ++ standard input/output application skills
- Differences between C ++ initialization and assignment
- Overview of C ++ header file content
- C ++ pointer drift Solution
Microsoft Excel, as a powerful spreadsheet processing software, has been widely used. In the process of database application software development, if you can exchange data between databases and Excel files, you can increase the source of database data and facilitate further processing of database data.
C ++ Builder is a visual and Rapid Application Development Tool. It provides an OLE Automation mechanism that allows developers to call Excel in applications to exchange data.
To call Excel in C ++ Builder, you must first create an OLE object for Excel, and then manipulate Excel by setting the properties of the object and calling the method of the object. C ++ Builder uses CreateOleObject () to create an OLE object. OlePropertySet (propname, value) is used to set the properties of the OLE object. OlePropertyGet (propname) is used to obtain the properties of the OLE object; use OleFunction (oleFuncName, [val,...]) and OleProcedure (oleProcName, [val,...]) to call the method of the OLE object.
C ++ Builder uses OLE to automate Excel operations. You must master Excel automation objects and VBA's methods and attributes about Excel objects. These are all in Microsoft Office (fully installed) in the VBAXL8.HLP help file. The following example shows how to operate an Excel table in C ++ Builder by converting the data in the database to an Excel worksheet.
Create a new form Form1, save the unit file Unit1.cpp, and save the project file Project1.bpr. Add the Data Access Control TTable to the form, set the Name attribute to Table1, The DatabaseName attribute to BCDEMOS, And the TableName attribute to Country. db. Add a button control TButton to the form and set its Name attribute to Button1 and Caption attribute to "convert to Excel File ". Double-click Button1 and add the following code to the Button1Click () function:
- Variant ex, newxls;
- Int I, j = 1;
- Try
- {
- Ex = CreateOleObject ("Excel. Application"); // start Excel
- }
- Catch (...)
- {
- ShowMessage ("cannot start Excel ″);
- }
- Ex. OlePropertySet ("Visible", (Variant) true); // Visible after Excel is started
- Newxls = (ex. OleFunction ("Workbooks"). OleFunction ("Add ″);
// Create a new workbook
- Table1-> Active = true;
- // Open the database
- Table1-> First ();
- For (I = 0; I <Table1-> FieldCount; I ++)
- // Write the field name to the first line of the workbook
- {
- (Ex. OleFunction ("Cells"). OlePropertySet ("Item", (Variant) 1,
(Variant) (I + 1), (Variant) Table1-> Fields [I]-> FieldName );
- }
- While (! Table1-> Eof)
- // Write the records in the database to the workbook in sequence
- {
- Jj = j + 1;
- For (I = 0; I <Table1-> FieldCount; I ++)
- {
- (Ex. OleFunction ("Cells"). OlePropertySet
("Item", (Variant) j, (Variant) (I + 1 ),
- (Variant) Table1-> Fields [I]-> AsString );
- }
- Table1-> Next ();
- }
- Newxls. OleFunction ("SaveAs", (Variant) filename );
- // Save the workbook. filename is the full file name of the workbook.
- Ex. OleFunction ("Quit ″);
- // Exit Excel and release the OLE object
Note: To use OLE automation objects, you must add # include "ComObj. hpp" to the Unit1.cpp file before compiling ″.
You can run the program to convert data in the database to an Excel worksheet. Similarly, you can use the OlePropertyGet () function to read the data in the Excel worksheet to the database.
The above C ++ Builder operating Excel table code was lowered in Windows 98 operating system and C ++ Builder 3.0 and passed the trial run.