I also wrote an article BCB called Excel on CSDN. The thought and Stationmaster's "uses OLE to operate Excel (C + + Builder Edition)" is the same.
If we use the 2K operating system, we can find that if only the EXCELAPP.PR ("Quit") is used, the Excel thread is still there, so that if the application does not quit, the called Excel file cannot be opened in Excel under the shell. So my BCB call Excel is like this in the end to add a little bit of work, is to put all the variant variables are set to unassigned, a small example of me as follows, a new Excel file, showing how many forms (sheet), Then add a form and name test, and assign a value to the (1,2) cell. Finally deletes a sheet.
==============================================================================
H file Definition macros
#define OPG Olepropertyget
#define OPS Olepropertyset
#define OFN Olefunction
#define OPR Oleprocedure
#define PR Procedure
To add a variable:
Variant Axl,workbook,axsheet,nms,bef,aft;
CPP function
void __fastcall Tform1::button1click (tobject *sender)
{
Assignment content
Ansistring s= "hello!";
File path and save name
Ansistring Filename=getcurrentdir () + "\\test.xls";
To determine whether a file exists
if (fileexists (filename))
{
Ask if you want to delete
if (Messagedlg ("File already exists, do you want to delete it?") ",
Mtwarning, Tmsgdlgbuttons () << mbok<<mbcancel, 0) ==mrok)
DeleteFile (filename);
Else
{ShowMessage ("abort action");
}
Building an Excel connection
Axl= variant::createobject ("Excel.Application");
Axl.ops ("Visible", false);
workbook= AXL.OPG ("workbooks");
Create a new Excel job (file)
Workbook.exec (PR ("Add"));
WORKBOOK=AXL.OPG ("ActiveWorkbook");
Shows the number of sheet that exist
int COUNT=WORKBOOK.OPG ("sheets"). OPG ("Count");
ShowMessage (IntToStr (count) + "table");
Add a sheet, named test
AFT=WORKBOOK.OPG ("Sheets", count);
WORKBOOK.OPG ("Sheets"). OPR ("Add", bef. Noparam (), aft);
AXSHEET=WORKBOOK.OPG ("ActiveSheet");
Rename
Axsheet.ops ("Name", "Test");
Assign value to Unit 1, 2
AXSHEET.OPG ("Cells")
. OPG ("Item", (variant) 1, (variant) 2)
. OPS ("Value", S.c_str ());
. Exec (PropertySet ("Value") << s.c_str ());
Both of these options are available
Choose the first one sheet
int sheetnum=1;
AXSHEET=WORKBOOK.OPG ("Sheets", sheetnum);
Select a form with the name Sheet2
Ansistring shname= "Sheet2";
AXSHEET=WORKBOOK.OPG ("Sheets", shname.c_str ());
Turn off warning Tips
WORKBOOK.OPG ("Application"). OPS ("DisplayAlerts", false);
Delete the selected form
Axsheet.ofn ("Delete");
Turn on warning tips
WORKBOOK.OPG ("Application"). OPS ("DisplayAlerts", false);
Save the file in either of two ways
Workbook.exec (PR ("SaveAs") <<filename);
WORKBOOK.OPR ("SaveAs", Filename.c_str ());
End exit
WORKBOOK.OPR ("close");
Axl.ofn ("Quit");
End, the Excel thread does not end until the application exits without the following code.
axl=unassigned;
workbook=unassigned;
axsheet=unassigned;
bef=unassigned;
aft=unassigned;
nms=unassigned;
ShowMessage ("OK done boy!");