How to use C ++ Builder 4.0 Enterprise to implement the call to Excel97

zhaozj2021-02-08  256

In the development of database software, we generally have a large number of report designs, although we can use the Crystal Report or program comes with report tools, but when we involve designed multiple reports or cross-reports, we generally I will feel that I don't have my heart. Sometimes I want to use Excel as a reception report, but I can't find the relevant interface. In fact, we only need to know the interface structure of Excel, you can easily achieve calls to Excel.

Principle: Program interface in Excel is generally divided into 3 floors, respectively: ExeLApplication, ExcelBook, Excelsheet where ExeLApplication represents Excel programs, ExcelBook represents the current work of Excel program, excelsheet represents the current activated form of ExcelBook, so when starting the Excel program To start according to this order, you can implement the operation of the Excel report.

Examples are now described as follows:

First, add excel8.olb in Import Type Library, generate an Excel_TLB.H file in the / include subdirectory.

Second, add Button1, Button2, Button3, Table1 in Form1 in BCB4.0, and store the disk to Project1.

Third, join the header file #include "../excel_tlb.h" in unit1.h ../excel_tlb.h "

Add to PRIVATE

Private:

Tcom_Application Application; File: // Defines Excelapplication object //

Worksheetptr worksheet; // Define Excelsheet objects //

RangePtr Firstcol; File: // Defines the column object //

RangePtr Range File: // Defines Table Operation Range //

Fourth, add the following code in the Button1 onclikc event:

Void __fastcall tform1 :: button1click (Tobject * Sender)

{// Start Excel //

Const int xlwbatchart = -4109;

Const Int XLWBATWORKSHEET = -4167;

IF (! application)

Application = coapplication _ :: create (); file: // is built on the EXCEL program //

Application-> set_visible (0, true); // Open Excel program //

Application-> Workbooks-> add (xlwbatworksheet); // Create exclobook // //

Worksheet = Application-> workbooks-> get_item (1) -> worksheets-> get_item (1); // get the table object //

Worksheet-> name = WideString ("Database Date"); // Establish the name of the table //

}

5. Add the following code in the Button2 onclikc event:

Void __fastcall tform1 :: button2click (Tobject * Sender)

{//adding data//

INT I, J;

Table1-> DatabaseName = "dbdemos";

Table1-> TableName = "author.db";

TABLE1-> open ();

For (i = 0; i fieldcount; i ) Worksheet-> Cells-> set__default (1, i, table1-> fileds-> fileds [i] -> filedname); // Add field in the specified location name//

Table1-> first ();

J = 2;

While (! Table1-> EOF ())

{

For (i = 0; i fieldcount; i )

Worksheet-> Cells-> set__default (j, i, table1-> fields-> fileds [i] -> asstring; add the content of the database at the specified location ///

Table1-> next ();

J ;

}

}

Sixth, add the following code in the Button3 onclikc event:

Void __fastcall tform1 :: button3click (TOBJECT * Sender)

{// Table setting ///

Range = m_worksheet-> get_range ("c1: f20"); // Set the table operation range //

Range-> font-> size = 12; // Set the font size //

Range-> columns-> interior-> colorindex = 3; // Set the table table color ///

Range-> borders-> lineestyle = xlcontinuous; // Set the table border //

Firstcol = m_worksheet-> columns-> get__default (3); // get the third column object of the current table //

Firstcol-> columnwidth = 25; // Set the object width //

Firstcol-> font-> bold = true; // Setting the font properties is bute //

Firstcol-> font-> italic = true; file: // Set the type of font //

Firstcol-> font-> color = clblue; // Set the color of the font ////

}

The above program is implemented in C Builder 4.0 Enterprise PWIN98.

It can be seen from the above programs that as long as we make a clever setting for Excel in the program, it is possible to design a professional level.

(Author: Sunhang Dong)

转载请注明原文地址:https://www.9cbs.com/read-1273.html

New Post(0)