Home > Database > Mysql Tutorial > QOdbc读写excel

QOdbc读写excel

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:55:34
Original
1911 people have browsed it

#include QApplication #include QSqlDatabase #include QDebug #include QMessageBox #include QSqlQuery #include QVariant #include QTime #include QTextCodec void writeExcel(QString excelFilePath) { // 创建一个数据库实例, 设置连接字符串 QSqlDa

#include

#include <QSqlDatabase>
Copy after login
#include <QDebug>
Copy after login
#include <QMessageBox>
Copy after login
#include <QSqlQuery>
Copy after login
#include <QVariant>
Copy after login
#include <QTime>
Copy after login
#include <QTextCodec>
Copy after login
void writeExcel(QString excelFilePath)
Copy after login
{
Copy after login
Copy after login
Copy after login
    // 创建一个数据库实例, 设置连接字符串
Copy after login
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","excelexport");
Copy after login
Copy after login
    QString dsn = QString("DRIVER={Microsoft Excel Driver (*.xls)};DSN=&#39;&#39;; FIRSTROWHASNAMES=1; READONLY=FALSE;CREATE_DB=\"%1\";DBQ=%2").
Copy after login
            arg(excelFilePath).arg(excelFilePath);
Copy after login
    db.setDatabaseName(dsn);
Copy after login
    // 打开数据库
Copy after login
Copy after login
    if (!db.open())
Copy after login
Copy after login
    {
Copy after login
Copy after login
Copy after login
        qDebug()<< "open false";
Copy after login
Copy after login
        QMessageBox::about(NULL, "r", "open false");
Copy after login
Copy after login
    }
Copy after login
Copy after login
Copy after login
Copy after login
    // 创建表格
Copy after login
    QString sql = "create table sheet (name TEXT, age NUMBER)";
Copy after login
    QSqlQuery query(db);
Copy after login
    if (!query.exec(sql))
Copy after login
    {
Copy after login
Copy after login
Copy after login
        qDebug()<< "create table false!";
Copy after login
        QMessageBox::about(NULL, "r", "create table false!");
Copy after login
    }
Copy after login
Copy after login
Copy after login
Copy after login
    // 写入数据
Copy after login
    db.exec( "insert into sheet(name, age) values(&#39;ctb&#39;, &#39;28&#39;)");
Copy after login
    db.exec( "insert into sheet(name, age) values(&#39;xw&#39;, &#39;19&#39;)");
Copy after login
    db.exec( "insert into sheet(name, age) values(&#39;lg&#39;, &#39;34&#39;)");
Copy after login
    // 关闭数据库
Copy after login
Copy after login
    db.close();
Copy after login
Copy after login
}
Copy after login
Copy after login
Copy after login
void readExcel(const QString excelPath)
Copy after login
{
Copy after login
Copy after login
Copy after login
    // excel 数据库连接字符串 需要QODBC 驱动
Copy after login
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","excelexport");
Copy after login
Copy after login
    QString connString = QString("Driver={Microsoft Excel Driver (*.xls)};Readonly=1;DriverId=790;Dbq=%1;DefaultDir=D:\\").arg(excelPath);
Copy after login
    db.setDatabaseName(connString);
Copy after login
    // 打开数据库
Copy after login
Copy after login
    if (!db.open())
Copy after login
Copy after login
    {
Copy after login
Copy after login
Copy after login
        qDebug()<< "open false";
Copy after login
Copy after login
        QMessageBox::about(NULL, "r", "open false");
Copy after login
Copy after login
    }
Copy after login
Copy after login
Copy after login
Copy after login
    //查询数据
Copy after login
    QString sql = "Select * from [sheet$]";
Copy after login
    QSqlQuery query(sql, db);
Copy after login
    while (query.next()) {
Copy after login
        //读取数据
Copy after login
        QString name = query.value(0).toString();
Copy after login
        int age = query.value(1).toInt();
Copy after login
        qDebug()<< name << age <<endl;
Copy after login
    }
Copy after login
Copy after login
Copy after login
Copy after login
    // 关闭数据库
Copy after login
Copy after login
    db.close();
Copy after login
Copy after login
}
Copy after login
Copy after login
Copy after login
int main(int argc, char *argv[])
Copy after login
{
Copy after login
Copy after login
Copy after login
    QApplication a(argc, argv);
Copy after login
    //中文支持
Copy after login
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("system"));
Copy after login
    QTextCodec::setCodecForTr(QTextCodec::codecForName("system"));
Copy after login
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("system"));
Copy after login
    writeExcel("D:\\test01.xls");
Copy after login
    readExcel("D:\\test01.xls");
Copy after login
    return a.exec();
Copy after login
}
Copy after login
Copy after login
Copy after login
写入结果:
Copy after login
读取打印结果
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template