How to use C + + Builder to implement the call to EXCEL97

Source: Internet
Author: User
Tags join range

We are in the development of database software, generally have to carry out a large number of report design, although we can use the Crystal report or the process of reporting tools to carry out the report design, but when it comes to the design of multiple reports or cross statements, we generally feel powerless. Sometimes, although you want to use Excel as the foreground report, you can't find the relevant interface. In fact, as long as we know the interface structure of Excel, we can easily implement the call to Excel.

Principle: In Excel, the program interface is generally divided into 3 layers, namely: Exelapplication, Excelbook, ExcelSheet which exelapplication represents the Excel program, Excelbook represents the current working copy of the Excel program, ExcelSheet represents the currently active table for Excelbook, so start the Excel program in this order, so that you can implement the Excel report.

Examples are as follows:

First, add Excel8.olb to the import type library and generate the Excel_tlb.h file in the \include subdirectory.

Second, in the bcb4.0 Form1 add button1, Button2, Button3, table1, and parallel disk for Project1.

Third, in the Unit1.h to add the header file #include ". \excel_tlb.h "

Join in private

Private

Tcom_application Application; file://define ExcelApplication Objects//

WORKSHEETPTR worksheet; Define ExcelSheet Object//

Rangeptr Firstcol; file://Define Column objects//

Rangeptr range file://Define table action Range//

Iv. Add the following code to the ONCLIKC event in Button1:

void __fastcall Tform1::button1click (tobject *sender)
{//Start excel//
const int Xlwbatchart =-4109;
const int xlwbatworksheet =-4167;
if (! Application)
application = Coapplication_::create (); file://connections built in Excel programs//
Application->set_visible (0, true); Open an Excel program//
Application->workbooks->add (Xlwbatworksheet); Create a excelbook//that contains only one ExcelSheet
Worksheet = Application->workbooks->get_item (1)->worksheets->get_item (1); Get the Table object//
Worksheet->name = widestring ("Database Date"); Create the name of the table//
}

V. Add the following code to the ONCLIKC event in Button2:

void __fastcall Tform1::button2click (tobject *sender)
{//Add data//
int I, J;
Table1->databasename= "Dbdemos";
Table1->tablename= "Author.db";
Table1->open ();
for (i=0;i<table1->fieldcount;i++)
Worksheet->cells->set__default (1,i,table1->fileds->fileds[i]->filedname); Adds a field name to the specified position//
Table1->first ();
j=2;
while (!table1->eof ())
{
for (i=0;i<table1->fieldcount;i++)
Worksheet->cells->set__default (J,i, table1->fields->fileds[i]->asstring); Join the contents of the database at the specified location//
Table1->next ();
j + +;
}
}

In the Button3 ONCLIKC event, add the following code:

void __fastcall Tform1::button3click (tobject *sender)
{//Table settings//
Range = M_worksheet->get_range ("C1:f20"); Set table action Range//
range->font->size=12; Set Font size//
Range->columns->interior->colorindex = 3; Set Table Color//
Range->borders->linestyle = xlcontinuous; Set table Border//
Firstcol = M_worksheet->columns->get__default (3); Get the third column object in the current table//
Firstcol->columnwidth = 25; Set Object width//
Firstcol->font->bold = true; Set font properties to bold//
Firstcol->font->italic = true; file://sets 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.

Through the above procedures can be seen, as long as we are in the program of Excel clever settings, we can design a professional level of the report.

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.