QT uses ODBC to manipulate Excel

Source: Internet
Author: User

Example code:
  
 
  1. #include <QtCore/QCoreApplication>
  2. #include <QtSql>
  3. #include <QObject>
  4. #include <qdebug.h>
  5. int main(int argc, char *argv[])
  6. {
  7. QCoreApplication a(argc, argv);
  8. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
  9. if(!db.isValid())
  10. {
  11. qDebug() << "Type Error!";
  12. return 0;
  13. }
  14. /*
  15. * Excel 2003
  16. *db.setDatabaseName("Driver={Microsoft Excel Driver (*.xls)};Readonly=0;DriverId=790;Dbq=d:\\temp\\book1.xls;DefaultDir=d:\\temp");
  17. */
  18. /*
  19. *Excel 2007及以上
  20. */
  21. QString connString = QString("Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%1")
  22. .arg("D:\\ProgramProject\\ODBC_Excel\\ODBC_Excel\\data.xlsx");
  23. db.setDatabaseName(connString);
  24. /*
  25. *其他操作与数据库的操作是一样的
  26. */
  27. if ( db.open())
  28. {
  29. qDebug() << "Open Excel Successful!";
  30. QSqlQuery query(db);
  31. query.exec("select name,age from [sheet1$]");
  32. while (query.next())
  33. {
  34. QString strName = query.value(0).toString();
  35. QString strAge = query.value(1).toString();
  36. qDebug() << strName << strAge;
  37. }
  38. }
  39. else
  40. qDebug() << db.lastError().text();
  41. /*
  42. *善后操作
  43. */
  44. db.close();
  45. db.removeDatabase(connString);
  46. return 0;
  47. //return a.exec();//不进入事件循环,直接结束程序
  48. }


1, Consider an Excel file (for example, Book.xls) as a database in which each worksheet (sheet) is considered a database table. Assume that the first behavior field name in Excel, so you must include the contents of the first row in the scope that you define.

< Span style= "FONT-SIZE:14PX; Font-family:simsun; line-height:21px; " >2, the name of the table must be added $ and the square brackets on both sides ( insert a row of data, Standard SQL statement, table name must be in [Excel file name $] format, reading data from Excel tables, standard SQL statements, table names must be in [table name $] Format
 
   
  
  1. query exec ( "CREATE TABLE Guest (visitor text,age int,comments text)" //this is the action to create the table
  2. query exec ( "INSERT into [sheet1$] values (1, ' Ane Brun ', 7)" //this is the action inserted












From for notes (Wiz)

QT uses ODBC to manipulate Excel

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.