QOdbc read/write excel

Source: Internet
Author: User

#include <QApplication>
#include <QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include <QSqlQuery>
#include <QVariant>
#include <QTime>
#include <QTextCodec>
void writeExcel(QString excelFilePath)
{
// Create a database instance and set the connection string
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","excelexport");
    QString dsn = QString("DRIVER={Microsoft Excel Driver (*.xls)};DSN=''; FIRSTROWHASNAMES=1; READONLY=FALSE;CREATE_DB=\"%1\";DBQ=%2").
            arg(excelFilePath).arg(excelFilePath);
    db.setDatabaseName(dsn);
// Open the database
    if (!db.open())
    {
        qDebug()<< "open false";
        QMessageBox::about(NULL, "r", "open false");
    }
// Create a table
    QString sql = "create table sheet (name TEXT, age NUMBER)";
    QSqlQuery query(db);
    if (!query.exec(sql))
    {
        qDebug()<< "create table false!";
        QMessageBox::about(NULL, "r", "create table false!");
    }
// Write data
    db.exec( "insert into sheet(name, age) values('ctb', '28')");
    db.exec( "insert into sheet(name, age) values('xw', '19')");
    db.exec( "insert into sheet(name, age) values('lg', '34')");
// Close the database
    db.close();
}
void readExcel(const QString excelPath)
{
// QODBC driver is required for the excel database connection string
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","excelexport");
    QString connString = QString("Driver={Microsoft Excel Driver (*.xls)};Readonly=1;DriverId=790;Dbq=%1;DefaultDir=D:\\").arg(excelPath);
    db.setDatabaseName(connString);
// Open the database
    if (!db.open())
    {
        qDebug()<< "open false";
        QMessageBox::about(NULL, "r", "open false");
    }
// Query data
    QString sql = "Select * from [sheet$]";
    QSqlQuery query(sql, db);
    while (query.next()) {
// Read data
        QString name = query.value(0).toString();
        int age = query.value(1).toInt();
        qDebug()<< name << age <<endl;
    }
// Close the database
    db.close();
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
// Chinese support
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("system"));
    QTextCodec::setCodecForTr(QTextCodec::codecForName("system"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("system"));
    writeExcel("D:\\test01.xls");
    readExcel("D:\\test01.xls");
    return a.exec();
}
Write result:

Read printed results:
 

Related Article

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.