Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reading excel file using QT



  • i tried below code, but its failed to open the .xslx file, can some please let me know where i am going wrong

    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "excelexport");
    	if (!db.isValid())
    	{
    		std::cout << "ExportExcelObject::export2Excel failed: QODBC not supported.";
    		return -2;
    	}
    	QString excelFilePath = "C:\\My_work\\Personal\\info.xlsx";
    
    	/* set the dsn string*/
    	QString dsn = QString("DRIVER={Microsoft Excel Driver (*.xls,*.xlsx)};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%1\";DBQ=%2").
    		arg(excelFilePath).arg(excelFilePath);
    	db.setDatabaseName(dsn);
    	if (!db.open())
    		std::cout << "failed to openDb" << std::endl;
    	else
    		std::cout << "Db Opened Success" << std::endl;
    


  • @Pradip-Shinde

    The documentation says:
    QODBC Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compliant databases

    ODBC driver might not be able to read EXCEL. I have heard of Excel to read through ODBC format, which does not necessarily mean that the ODBC driver can read Excel as well.

    You can also check the error return text with lastError and the member function text() This might give more explanations.



  • @koahnig thank for your input, do we have any option that we can read EXCEL file ?



  • @Pradip-Shinde

    I am not an expert in that area. I have found just now this Qt wiki

    To my surprise it uses also ODBC interface. You need to check there


Log in to reply