January 20, 2012

leo1756 leo1756
Lab Rat
7 posts

Qt and MS Excel

 

Hello everyone,

I’m writing an application in Qt that manipulates some Excel (.xls) files.
I could get it working and the instrunctions are executed succesfully. But I’m experiencing a problem when closing the application.
At the end of the instructions I call:

  1. workbook->dynamicCall("Close (Boolean)", false);
  2. excel->dynamicCall("Quit (void)");

but when I check the Windows Task Manager (CTRL+ALT+DEL) after executing the code I can still see the “EXCEL.exe” process at the process tab. Each time I run the instructions a new “EXCEL.exe” process appears.
Can someone please help me to solve this problem?

Thanks very much.

Leo

6 replies

January 22, 2012

Volker Volker
Robot Herder
5428 posts

Do you use Active Qt? Not that I’m familiar with that, but someone else might be.

January 23, 2012

leo1756 leo1756
Lab Rat
7 posts

Hello,

Yes, I’m using ActiveQt.
I’m posting my code below. The code is “silly” and probably could be better wiritten, but it performs the task I need. I’d just need to solve that problem when closing the Excel application.

  1. {
  2.     QAxObject* excel = new QAxObject( "Excel.Application", 0 );
  3.     QAxObject* workbooks = excel->querySubObject( "Workbooks" );
  4.     QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", "C:\\Users\\LocalDev\\teste.xls" );
  5.     QAxObject* sheets = workbook->querySubObject( "Worksheets" );
  6.  
  7.     sheets->dynamicCall("Add()");
  8.     QAxObject* sheet = sheets->querySubObject( "Item( int )", 1 );
  9.     sheet->setProperty("Name","Nova Planilha");
  10.  
  11.     QAxObject * range = sheet->querySubObject("Cells( int, int)",1,1);
  12.     range->setProperty("Value", QVariant(1234));
  13.  
  14.     QAxObject * shapes = sheet->querySubObject("Shapes");
  15.     shapes->dynamicCall("AddPicture( QString&, bool, bool, double, double, double, double","C:\\Users\\LocalDev\\Pictures\\cvrd1.png",true,true,100,100,70,70);
  16.  
  17.     excel->setProperty("DisplayAlerts", false);
  18.     excel->dynamicCall("Save()");
  19.  
  20.     //workbook->dynamicCall("Close()");
  21.     //excel->dynamicCall("Quit()");
  22.  
  23.     workbook->dynamicCall("Close (Boolean)", true);
  24.     excel->dynamicCall("Quit (void)");
  25.  
  26.     delete shapes;
  27.     delete range;
  28.     delete sheet;
  29.     delete sheets;
  30.     delete workbook;
  31.     delete workbooks;
  32.     delete excel;
  33. }

Thanks once again.

Leo

February 22, 2012

musto musto
Lab Rat
2 posts

I did run into hanging Excel Process when I had a similar code.
On Windows 7 Pro. with Office 2003 I don’t have this problem as long as I follow the order
Save(), Close(),Quit(),Delete()
just the same way as you do in your code.

I was once testing the Office 2010 and I remember I had to use SaveCopyAs

  1.      if(Office2010)
  2.          xlWBWholesale->dynamicCall("SaveCopyAs(const QString&)",getWholesalepath());
  3.      else
  4.          xlWBWholesale->dynamicCall("Save()");
  5.      xlWBWholesale->dynamicCall("Close()");

Though I can’t remember with certainty if that solved the issue you’re talking about.

February 23, 2012

leo1756 leo1756
Lab Rat
7 posts

Hello musto,

I will try that and post the result later.
Actually I’ve noticed that this problem is not specific to QT since I have seen people complaining about this issue on many foruns of other languages and platforms too.
Anyway I will try as you suggested and see if it works.

Thank you.

Leo

February 23, 2012

Skyrim Skyrim
Lab Rat
44 posts

Hi Leo
In this forum has already discussed Qt and Excel here [developer.qt.nokia.com].
I am using the following method:

  1.    wbooks->dynamicCall("Close()");
  2.    excel->dynamicCall("Quit()");
  3.    delete excel;

May 21, 2012

SKolaMunn SKolaMunn
Lab Rat
13 posts

Has anyone found a solution to this problem? I have the same problem, the “EXCEL.EXE” process remains in my Task Manager list of running processes after I call Close() and Quit() and delete everything. If I then externally (from my app) open the Excel file that I created, it opens “blank” (like corrupt, not fully drawn), then close Excel, then the process appears to properly close and when I open the file externally again, it is fine. I’d show a snippet of code but its pretty much the same as the snippet posted here by leo1756 on Jan 23, except instead of Close(true), I just use Close() and instead of Save(), I use SaveAs(const QString&) with the desired filename. I’ve read tons of posts about this all over the internet and on different forums but none of the solutions seem to be working for me.

 
  ‹‹ QGraphicsScene + subtract and move      Reentrancy ››

You must log in to post a reply. Not a member yet? Register here!