Qt reads Excel data through ODBC

Source: Internet
Author: User
Tags dsn

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.