PDA

View Full Version : Read excel file



ado130
15th August 2017, 10:15
Hello forum,
I have excel file with this pattern:


ID #1 #2 #3
NAME A B C
#1 A x
#2 B x
#3 C x

"X" means that the row belongs to the column.


QString path = fileName;
sheet = nullptr;
sheets = nullptr;
workbook = nullptr;
workbooks = nullptr;
excelApplication = nullptr;

if(path == "")
{
qDebug() << "'fileName' is empty!";
return;
}
path.replace("/", "\\");

excelApplication = new QAxObject( "Excel.Application", 0 );

if (excelApplication == nullptr)
throw invalid_argument("Failed to initialize interop with Excel (probably Excel is not installed)");

excelApplication->dynamicCall( "SetVisible(bool)", false ); // hide excel
excelApplication->setProperty( "DisplayAlerts", 0); // disable alerts

workbooks = excelApplication->querySubObject( "Workbooks" );
workbook = workbooks->querySubObject( "Open(const QString&)", path );
sheets = workbook->querySubObject( "Worksheets" );

...
...

for(int i=0; i<rowsCnt; ;++i)
for(int j=0; j<colsCnt; ;++j)
{
QAxObject* cell = sheet->querySubObject( "Cells( int, int )", i, j);
QVariant value = cell->dynamicCall( "Value()" );
dataRow.append(value);
}

It works fine, but the execution time is slow, especially at a larger table (200x200 or more).

My question is if exist faster way to read excel file.

Thanks for any advice!

d_stranz
15th August 2017, 17:09
If you could find a way to replace the calls to querySubObject() and dynamicCall() in the inner loop over the cells, that would probably speed things up considerably.

QAxObject is convenient, but if you can #import the Excel type libraries this would let you use the Excel COM objects directly without all the parsing overhead. Here's a link that might help (http://www.technical-recipes.com/2012/how-to-interface-with-excel-in-c/).

ado130
16th August 2017, 13:18
Thank you! I'll try the second option.