The traditional way of reading is through Excel.Application, this way not only cumbersome operation, but also the speed is not fast.
With ODBC reads, the entire worksheet can be read directly using the SELECT statement, and processing Excel data is as easy as a database.
Of course, there are also deficiencies in this approach:
1. Excel tables must have only one row of headers.
2, relative to Excel.Application, can not accurately locate the cell.
3, the worksheet name is equivalent to the database table name, the table header is equivalent to the field name, so Excel format must be fixed, otherwise unreadable to the data
Read the following code:
// file path QString FilePath; //Desktop Open
//Qt4//QString desktopdir=qdesktopservices::storagelocation (qdesktopservices::D esktoplocation);//Qt 5QString desktopdir=qstandardpaths::writablelocation (qstandardpaths::D esktoplocation); FilePath=qfiledialog::getopenfilename (Parent,"Select Excel", Desktopdir,"*.xls");if(Filepath.isnull ()) {error=" cannot open Excel file "; return;}//Read ExcelQsqldatabase db = Qsqldatabase::adddatabase ("Qodbc","Excel");if( !Db.isvalid ()) {Error="database-driven exceptions"; return;
" driver={microsoft Excel DRIVER (*.xls)}; " " dsn= ';D bq= "+filepath;db.setdatabasename (DSN); // Open Connection if ( ! Db.open ()) { Error=" Unable to open database "; return;
}qsqlquery query (DB); Qsqlrecord record;
"Sheet1 $ " // sheet name, $ is required QString SQL="select * FROM ["+tablename+"]";
Qt reads Excel data through ODBC