How to use C ++ Builder to Operate Excel tables

Source: Internet
Author: User

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:

 
 
  1. Variant ex, newxls;
  2. Int I, j = 1;
  3. Try
  4. {
  5. Ex = CreateOleObject ("Excel. Application"); // start Excel
  6. }
  7. Catch (...)
  8. {
  9. ShowMessage ("cannot start Excel ″);
  10. }
  11. Ex. OlePropertySet ("Visible", (Variant) true); // Visible after Excel is started
  12. Newxls = (ex. OleFunction ("Workbooks"). OleFunction ("Add ″);
    // Create a new workbook
  13. Table1-> Active = true;
  14. // Open the database
  15. Table1-> First ();
  16. For (I = 0; I <Table1-> FieldCount; I ++)
  17. // Write the field name to the first line of the workbook
  18. {
  19. (Ex. OleFunction ("Cells"). OlePropertySet ("Item", (Variant) 1,
    (Variant) (I + 1), (Variant) Table1-> Fields [I]-> FieldName );
  20. }
  21. While (! Table1-> Eof)
  22. // Write the records in the database to the workbook in sequence
  23. {
  24. Jj = j + 1;
  25. For (I = 0; I <Table1-> FieldCount; I ++)
  26. {
  27. (Ex. OleFunction ("Cells"). OlePropertySet
    ("Item", (Variant) j, (Variant) (I + 1 ),
  28. (Variant) Table1-> Fields [I]-> AsString );
  29. }
  30. Table1-> Next ();
  31. }
  32. Newxls. OleFunction ("SaveAs", (Variant) filename );
  33. // Save the workbook. filename is the full file name of the workbook.
  34. Ex. OleFunction ("Quit ″);
  35. // 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.

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.