| 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.ProgramDevelopment tool, which 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. In the following example, the data in the database is transferred to the Excel worksheet to illustrate how C ++ builder operates excel. 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 to the button1click () function:Code: 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 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 { J = 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 code above has been lowered for trial run in Windows 98 operating system and C ++ builder 3.0.
|