Question about link with Excel file
-
Recently I just want to load excel into Qt,and i used to have a post in Qt Forum,it didn't work out until now.
However,I found some code on the Internet says users can use QAxObject to solve this question,the code is like:QAxObject excel("Excel.Application");
It always starts like this,but I never succeed in this way,it reminds me it can't find any "Excel.Application" thing.What's more,I have checked my QAxObject contents,but I didn't find anything referred to "Excel.Application".
So anybody can help me about the Excel thing,after failure in ODBC and QAxObject,I doubted if there is some problem with my Excel Version or MS Office Software.@mrjj
-
Hi
Why not just test your excel?
You can try this sample and see.
Works here so should work at your place too.
https://www.dropbox.com/s/kxpei9wmx0v95rx/excel.zip?dl=0 -
If you need to read/write xlsx files you could try QtXlsxWriter. It does not require excel installed and performs all the basic tasks
-
@mrjj Sorry to interrupt you for such a long time:(
Now it can work(using QAxObject "Excel.Application" ),it can open a file and read back a caption ,but when I use DynamicCall() function,it usually reminds me of some mistakes(can't find the fun in the activce), I thought it could be related to the new version of excel and the function under activex has changed(I am a rookie...:()And should I find the VBA book to learn the newest operation of VBA?
And I can't open the dropbox link... :( Maybe it's time to buy a VPN...
-
Finally Solved.Luckily I found some posts told the reason----It seems the qt has to initialize and release OLE in a sub Thread(the GUI Thread has done this job so sometimes it may not show errors)
Here is the code:
#include <QCoreApplication> #include <QAxObject> #include "qt_windows.h" //Necessary #include <QVariant> #include <QDebug> int main(int argc, char *argv[]) { //Initialize OLE HRESULT r = OleInitialize(0); if (r != S_OK && r != S_FALSE) {qWarning("OLE initialize failure(error %x)",(unsigned int)r);} QCoreApplication a(argc, argv); QAxObject excel("Excel.Application"); excel.setProperty("Visible",false); QAxObject *workbooks = excel.querySubObject("WorkBooks"); workbooks->dynamicCall("Open (const QString&)",QString("d:/grade.xls")); QAxObject *workbook = excel.querySubObject("ActiveWorkBook"); QAxObject *worksheets = workbook->querySubObject("WorkSheets"); QAxObject *worksheet=worksheets->querySubObject("Item(int)",1); QAxObject *range; QString strVal; QAxObject *usedrange=worksheet->querySubObject("UsedRange"); int endrow=usedrange->querySubObject("Rows")->property("Count").toInt(); int endcolumn=usedrange->querySubObject("Columns")->property("Count").toInt(); for (int i=1;i<=endrow;i++){ for (int j=1;j<=endcolumn;j++){ range=worksheet->querySubObject("Cells(int,int)",i,j); strVal=range->dynamicCall("Value2()").toString(); qDebug()<<strVal; } qDebug()<<endl; } workbook->dynamicCall("Close()"); excel.dynamicCall("Quit()"); OleUninitialize(); return a.exec(); }