Board index » cppbuilder » Excel writing issues

Excel writing issues


2006-10-09 05:17:16 AM
cppbuilder15
I dont know exactly where to post this but here it goes. I have the
following code:
FileHandler::FileHandler(String filename)
{
fileXls = filename;
/*Initialise File*/
XLApp = Variant::CreateObject("excel.application"); //open excel
application
XLApp.OlePropertySet("visible",false); //set application to invisible
v0 = XLApp.OlePropertyGet("workbooks"); //get workbook
v0.OleProcedure("Add"); //add one workbook
v1 = v0.OlePropertyGet("Item",1); //select workbook number 1
v2 = v1.OlePropertyGet("WorkSheets"); //Get worksheet
v2.OleFunction("Add"); // add one work sheet
v3 = v2.OlePropertyGet("Item",1); //select worksheet number 1
v3.OlePropertySet("Name","Example"); //rename worksheet it to "example1?
}
FileHandler::~FileHandler()
{
}
void FileHandler::Write(double val,int row,int col)
{
char temp[10];
sprintf(temp,"%lf",val);
/*Write val to row, col position in excel*/
v3.OlePropertyGet("Cells").OlePropertyGet("Item",row,col).OlePropertySet("Value",temp);
/****Error****/
//this will stop asking you where you want to save your excel file or not
XLApp.OlePropertySet("DisplayAlerts",false);
//save your excel file at "d" and name it as "case1.xls"
XLApp.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs","C:\\TEST1.xls");
//Close the work book
v1.OleProcedure("Close");
//quite the excel application
XLApp.OleProcedure("Quit");
//unassign variable
XLApp=Unassigned;
}
If i have a loop like this
for(int i=1;i<20;i++)
write((double)i,i,1);
i get an error at "/****Error****/" when i hits 2. Can anyone tell me why
Thanks Michael
 
 

Re:Excel writing issues

Hello Michael,
Note following statement in Write(),
XLApp=Unassigned;
so, the XLApp is freed in procedure Write().
and when the i hits 2, certainly, it will error.
"Michael" < XXXX@XXXXX.COM >写入消息新闻: XXXX@XXXXX.COM ...
Quote
I dont know exactly where to post this but here it goes. I have the
following code:

FileHandler::FileHandler(String filename)
{
fileXls = filename;
/*Initialise File*/
XLApp = Variant::CreateObject("excel.application"); //open excel
application
XLApp.OlePropertySet("visible",false); //set application to invisible
v0 = XLApp.OlePropertyGet("workbooks"); //get workbook
v0.OleProcedure("Add"); //add one workbook
v1 = v0.OlePropertyGet("Item",1); //select workbook number 1
v2 = v1.OlePropertyGet("WorkSheets"); //Get worksheet
v2.OleFunction("Add"); // add one work sheet
v3 = v2.OlePropertyGet("Item",1); //select worksheet number 1
v3.OlePropertySet("Name","Example"); //rename worksheet it to "example1?

}

FileHandler::~FileHandler()
{
}

void FileHandler::Write(double val,int row,int col)
{

char temp[10];
sprintf(temp,"%lf",val);
/*Write val to row, col position in excel*/

v3.OlePropertyGet("Cells").OlePropertyGet("Item",row,col).OlePropertySet("Value",temp);
/****Error****/
//this will stop asking you where you want to save your excel file or not
XLApp.OlePropertySet("DisplayAlerts",false);
//save your excel file at "d" and name it as "case1.xls"

XLApp.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs","C:\\TEST1.xls");
//Close the work book
v1.OleProcedure("Close");
//quite the excel application
XLApp.OleProcedure("Quit");
//unassign variable
XLApp=Unassigned;
}

If i have a loop like this
for(int i=1;i<20;i++)
write((double)i,i,1);

i get an error at "/****Error****/" when i hits 2. Can anyone tell me why

Thanks Michael
 

Re:Excel writing issues

:) Sorry I didnt even see that. It was pretty stupis but thanks for pointing
it out
"Michael" < XXXX@XXXXX.COM >wrote in message
Quote
I dont know exactly where to post this but here it goes. I have the
following code:

FileHandler::FileHandler(String filename)
{
fileXls = filename;
/*Initialise File*/
XLApp = Variant::CreateObject("excel.application"); //open excel
application
XLApp.OlePropertySet("visible",false); //set application to invisible
v0 = XLApp.OlePropertyGet("workbooks"); //get workbook
v0.OleProcedure("Add"); //add one workbook
v1 = v0.OlePropertyGet("Item",1); //select workbook number 1
v2 = v1.OlePropertyGet("WorkSheets"); //Get worksheet
v2.OleFunction("Add"); // add one work sheet
v3 = v2.OlePropertyGet("Item",1); //select worksheet number 1
v3.OlePropertySet("Name","Example"); //rename worksheet it to "example1?

}

FileHandler::~FileHandler()
{
}

void FileHandler::Write(double val,int row,int col)
{

char temp[10];
sprintf(temp,"%lf",val);
/*Write val to row, col position in excel*/

v3.OlePropertyGet("Cells").OlePropertyGet("Item",row,col).OlePropertySet("Value",temp);
/****Error****/
//this will stop asking you where you want to save your excel file or not
XLApp.OlePropertySet("DisplayAlerts",false);
//save your excel file at "d" and name it as "case1.xls"

XLApp.OlePropertyGet("Workbooks").OlePropertyGet("Item",1).OleProcedure("SaveAs","C:\\TEST1.xls");
//Close the work book
v1.OleProcedure("Close");
//quite the excel application
XLApp.OleProcedure("Quit");
//unassign variable
XLApp=Unassigned;
}

If i have a loop like this
for(int i=1;i<20;i++)
write((double)i,i,1);

i get an error at "/****Error****/" when i hits 2. Can anyone tell me why

Thanks Michael

 

{smallsort}