How to write output on excel file using Borland C++

After demonstrating the way on how to read input from excel file using Borland c++ builder, we are now going to illustrate a� way to write your output to excel file using the same language, c++.

Sometimes it is better to explain things through example, so here’s a good simple example to demonstrate the process.

Assuming you are going to write an output stored in variable ,x, say x=3.14 at row 2, column 3 of the excel file. Then, all you have to do is to use the following code.

CASE 1: You have to create a new excel file then write your output there

//declare variables
Variant XL,v0,v1,v2,v3;
double x=3.14;

//open excel application
XL=Variant::CreateObject(”excel.application”);

//set the application as invisible, you can reverse “false” to “true” to make it visible
XL.OlePropertySet(”Visible”,false);

//Get workbook
v0=XL.OlePropertyGet(”Workbooks”);

//add one work book
v0.OleProcedure(”Add”);

//select workbook number 1
v1=v0.OlePropertyGet(”Item”,1);

//Get worksheet
v2=v1.OlePropertyGet(”WorkSheets”);

// add one work sheet
v2.OleFunction(”Add”);

//select worksheet number 1
v3=v2.OlePropertyGet(”Item”,1);

//rename worksheet it to “example1″
v3.OlePropertySet(”Name”,”example1″);

// at last, write your output at row 2 column 3
char temp[10];
sprintf(temp,”%lf”,x);
v3.OlePropertyGet(”Cells”).OlePropertyGet(”Item”,2,3).OlePropertySet(”Value”,temp);

//this will stop asking you where you want to same your excel file or not
XL.OlePropertySet(”DisplayAlerts”,false);

//save your excel file at “d” and name it as “case1.xls”
XL.OlePropertyGet(”Workbooks”).OlePropertyGet(”Item”,1).OleProcedure(”SaveAs”,”d:\\case1.xls”);

//Close the work book
v1.OleProcedure(”Close”);

//quite the excel application
XL.OleProcedure(”Quit”);

//unassign variable
XL=Unassigned;

CASE 2: You want to write your output in an existing excel file named “case1.xls” located at “D:”

double x=3.14;
//declare variables
Variant XL,v0,v1,v2,v3;

//open excel application
XL=Variant::CreateObject(”excel.application”);

//set the application as invisible, you can reverse “false” to “true” to make it visible
XL.OlePropertySet(”Visible”,false);

//open the workbook� “named case1.xls”
XL.OlePropertyGet(”Workbooks”).OleProcedure(”Open”,”d:\\case1.xls”);

//open the worksheet “example1″
v0=XL.OlePropertyGet(”Sheets”,”example1″);

//get a cell
v1=v0.OlePropertyGet(”Cells”);

// at last, write your output at row 2 column 3
char temp[10];
sprintf(temp,”%lf”,x);
v1.OlePropertyGet(”Item”,2,3).OlePropertySet(”Value”,temp);

//this will stop asking you where you want to same your excel file or not
XL.OlePropertySet(”DisplayAlerts”,false);

//save what you have done
XL.OleProcedure(”Save”);

//quite the excel application
XL.OleProcedure(”Quit”);

//unassign variable
XL=Unassigned;

That’s it. Though long, the procedure is still standard and simple.




« How to Read Input from Excel File Using Borland C++ Builder

Matrix TCL Lite: A Matrix Header File for Matrix Operations in C++ »