PDA

View Full Version : is it possible to load QAxobject Excel to initialize and call it later?



ofbrc
13th September 2019, 14:23
I am creating a program that will have the need to call the QAXobject Excel function on different circumstances ( creating , parsing, modifying ) and it works ,
but it always takes a lot of time loading whenever i call a certain process especially if i have to read/write a 200rows x 50 columns excel file .

i wonder if it will be possible to call the QAXobject Excel application from the start of the program?
leave it open and use it when needed ( when parsing rows , saving xls, modifying )

it is like the idea to call the code from the start:


QAxObject* excel = new QAxObject("Excel.Application");
QAxObject* workbooks = excel->querySubObject("WorkBooks");


and just use the following code when needed
code example is setting a value of a cell



workbooks->querySubObject("Open (const QString&)", filePath);
QAxObject* workbook = excel->querySubObject("ActiveWorkBook")
QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1);
QAxObject* cell = worksheet->querySubObject( "Cells( int, int )", rowids, coli);
cell->setProperty("Value", ShipAddi);

d_stranz
13th September 2019, 17:00
It should be possible to do all of this. I have done it in the past with other QAx objects for Word and other ActiveX / COM objects.

Just remember to match any call that creates a COM instance with a call to delete or close it so the objects (and the files they represent) don't get locked open when your program exits. The easiest way is to use smart pointers wherever possible.

Note that you are not limited to using QAxObject if you want to use Excel or other ActiveX objects in your Qt programs. Qt is just C++, so anything you can do in an ordinary C++ program you can also do in a Qt-based C++ program.

You can use the #import directive in your source code to create ".tlb" and ".tli" files that directly create C++ smart pointer interfaces to the ActiveX objects.

Here is one link (https://www.technical-recipes.com/2012/how-to-interface-with-excel-in-c/) that explains how to do that. And here's a link (https://personalpages.manchester.ac.uk/staff/Andrew.Hazel/EXCEL_C++.pdf) to a PDF document that gives examples starting on page 53.

In doing it directly as described in the links, you are using smart pointers that will "clean up" after themselves and ensure that everything you access will be properly closed or destroyed.