四、導(dǎo)出EXCEL表
最后,就是導(dǎo)出excel了,創(chuàng)建一個新的類
odbcexcel.h文件
#ifndef ODBCEXCEL_H
#define ODBCEXCEL_H
#include
#include
#include
#include
#include
#include
class OdbcExcel
{
public:
OdbcExcel();
//將數(shù)據(jù)保存為excel
bool static save(QString filePath,QStringList headers,QList data,QString comment="") ;
//將QTableView保存為excel
bool static saveFromTable(QString filePath,QTableView *tableView,QString comment="");
//獲取錯誤信息
QString static getError(){return error;}
private:
void static printError( QSqlError error);
bool static insert(QSqlQuery& query, QString sheetName, QStringList slist);
static QString error;
};
#endif // ODBCEXCEL_H
odbcexcel.c
#include "odbcexcel.h"
#include "odbcexcel.h"
#include
OdbcExcel::OdbcExcel()
{
}
QString OdbcExcel::error;
bool OdbcExcel::save(QString filePath, QStringList headers, QList data,QString comment)
{
QString sheetName = "Sheet1";
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC","excelexport");
if( !db.isValid())
{
error="數(shù)據(jù)庫驅(qū)動異常";
return false; //! type error
}
QString dsn = "DRIVER={Microsoft Excel Driver (*.xls)};"
"DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\""+filePath+"\\";DBQ="+filePath;
db.setDatabaseName( dsn);
// open connection
if( !db.open())
{
error="無法打開數(shù)據(jù)庫";
return false; //! db error
}
QSqlQuery query(db);
QString sql;
// drop the table if it's already exists
sql = QString("DROP TABLE [%1]").arg(sheetName);
query.exec( sql);
//create the table (sheet in Excel file)
sql = QString("CREATE TABLE [%1] (").arg(sheetName);
foreach (QString name, headers) {
sql +=QString("[%1] varchar(200)").arg(name);
if(name!=headers.last())
sql +=",";
}
sql += ")";
query.prepare( sql);
if( !query.exec()) {
OdbcExcel::printError( query.lastError());
db.close();
return false;
}
foreach (QStringList slist, data) {
insert(query,sheetName,slist);
}
if(!comment.isEmpty())
{
QStringList slist;
slist<for(int i=0,n=headers.size()-1;i<n;i++)
{
slist<<"";
}
insert(query,sheetName,slist);
}
db.close();
return true;
}
bool OdbcExcel::saveFromTable(QString filePath,QTableView *tableView, QString comment)
{
QAbstractItemModel* model=tableView->model();
const int column=model->columnCount();
const int row=model->rowCount();
//header
QStringList headers;
for(int i=0;i//隱藏列
if(tableView->isColumnHidden(i))
continue;
headers<headerData(i,Qt::Horizontal).toString();
}
//data
QStringList list;
QList data;
for(int i=0;iif(model->index(i,0).data().isNull())
continue;
list.clear();
for(int j=0;j//隱藏列
if(tableView->isColumnHidden(j))
continue;
list<index(i,j).data().toString();
}
data<<list;
}
return OdbcExcel::save(filePath,headers,data,comment);
}
void OdbcExcel::printError(QSqlError error)
{
QString sqlerr = error.text();
error=sqlerr;
qCritical()<query, QString sheetName, QStringList slist)
{
QString sSql = QString("INSERT INTO [%1] VALUES(").arg( sheetName);
for(int i=0,n=slist.size();i<n;i++)
{
sSql+=QString(":%1").arg(i);
if(i!=n-1)
sSql+=",";
else
sSql+=")";
}
query.prepare( sSql);
for(int i=0,n=slist.size();i<n;i++)
{
query.bindValue(QString(":%1").arg(i),slist.at(i));
}
if( !query.exec()) {
printError( query.lastError());
return false;
}
return true;
}
然后在UI界面中創(chuàng)建一個導(dǎo)出按鍵,并在按鍵按下時(shí)調(diào)用數(shù)據(jù)庫導(dǎo)出類來完成導(dǎo)出excel功能
//導(dǎo)出
void sqlite_sqlquerymodel_edit::on_pushButton_export_clicked()
{
QFileDialog dlg;
dlg.setAcceptMode(QFileDialog::AcceptSave);
// Qt 5
dlg.setDirectory(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation));
// Qt 4
// dlg.setDirectory(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation));
dlg.setNameFilter("*.xls");
dlg.selectFile(QDate::currentDate().toString("yyyy-MM-dd.xls"));
if(dlg.exec()!= QDialog::Accepted)
return;
QString filePath=dlg.selectedFiles()[0];
if(OdbcExcel::saveFromTable(filePath,ui->tableView,"注釋:無")) {
QMessageBox::information(this,tr("提示"),tr("保存成功"));
}
else{
QString msg="保存失??!\\n\\r"+OdbcExcel::getError();
QMessageBox::critical(this,tr("錯誤"),tr(msg.toUtf8()));
}
}
點(diǎn)擊導(dǎo)出后,彈出保存文件框保存文件
保存成功
然后打開excel查看保存的數(shù)據(jù)跟Qt上的一致
OK,Qt操作數(shù)據(jù)庫就到這了。
聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。
舉報(bào)投訴
-
API
+關(guān)注
關(guān)注
2文章
1465瀏覽量
61676 -
SQL
+關(guān)注
關(guān)注
1文章
751瀏覽量
43986 -
數(shù)據(jù)庫
+關(guān)注
關(guān)注
7文章
3737瀏覽量
64173 -
SQlite
+關(guān)注
關(guān)注
0文章
78瀏覽量
15872
發(fā)布評論請先 登錄
相關(guān)推薦
數(shù)據(jù)庫系統(tǒng)是什么?數(shù)據(jù)庫系統(tǒng)概念之數(shù)據(jù)庫設(shè)計(jì)資料免費(fèi)下載
什么是概念結(jié)構(gòu)設(shè)計(jì)1.將需求分析得到的用戶需求抽象為信息結(jié)構(gòu)即概念模型的過程就是概念結(jié)構(gòu)設(shè)計(jì)2.概念結(jié)構(gòu)是各種數(shù)據(jù)模型的共同基礎(chǔ),它比
發(fā)表于 09-07 14:34
?1次下載
如何進(jìn)行數(shù)據(jù)庫設(shè)計(jì)?數(shù)據(jù)庫設(shè)計(jì)介紹和需求分析及結(jié)構(gòu)設(shè)計(jì)資料概述
數(shù)據(jù)庫設(shè)計(jì)的任務(wù)是指根據(jù)需求研制數(shù)據(jù)庫結(jié)構(gòu)并應(yīng)用 數(shù)據(jù)庫的過程。數(shù)據(jù)庫設(shè)計(jì)內(nèi)容包括數(shù)據(jù)庫的
發(fā)表于 09-13 17:05
?0次下載
如何使用PowerDesigner進(jìn)行數(shù)據(jù)庫靜態(tài)結(jié)構(gòu)設(shè)計(jì)?詳細(xì)資料概述
把用戶需求抽象為概念模型即為概念結(jié)構(gòu)設(shè)計(jì)。
概念模型除了要求能反映客觀世界并且易于理解外,還要求其易于向數(shù)據(jù)模型(如關(guān)系模型)轉(zhuǎn)化。
概念模型獨(dú)立于具體的數(shù)據(jù)庫系統(tǒng),是整個數(shù)據(jù)庫設(shè)
發(fā)表于 09-13 17:05
?0次下載
數(shù)據(jù)庫靜態(tài)結(jié)構(gòu)如何設(shè)計(jì)?詳細(xì)資料任務(wù)和方法說明
任務(wù):實(shí)現(xiàn)數(shù)據(jù)庫設(shè)計(jì)新奧爾良方法中概念結(jié)構(gòu)設(shè)計(jì)和邏輯結(jié)構(gòu)設(shè)計(jì)
發(fā)表于 09-27 15:32
?1次下載
數(shù)據(jù)庫教程之如何進(jìn)行數(shù)據(jù)庫設(shè)計(jì)
本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)據(jù)庫教程之如何進(jìn)行數(shù)據(jù)庫設(shè)計(jì)內(nèi)容包括了:1 數(shù)據(jù)庫設(shè)計(jì)概述 ,2 數(shù)據(jù)庫需求分析 ,3 數(shù)據(jù)庫
發(fā)表于 10-19 10:41
?21次下載
數(shù)據(jù)庫設(shè)計(jì)的七大知識點(diǎn)總結(jié)詳細(xì)資料免費(fèi)下載
本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)據(jù)庫設(shè)計(jì)的七大知識點(diǎn)總結(jié)包括了:1 數(shù)據(jù)庫設(shè)計(jì)概述2 需求分析3 概念結(jié)構(gòu)設(shè)計(jì)4 邏輯結(jié)構(gòu)設(shè)計(jì)5 數(shù)據(jù)庫的物理
發(fā)表于 10-19 10:41
?0次下載
數(shù)據(jù)庫學(xué)習(xí)入門資料之如何進(jìn)行數(shù)據(jù)庫設(shè)計(jì)
本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)據(jù)庫學(xué)習(xí)入門資料之如何進(jìn)行數(shù)據(jù)庫設(shè)計(jì)主要內(nèi)容包括了:數(shù)據(jù)庫系統(tǒng)設(shè)計(jì)的1 概述2 需求分析3 概念設(shè)計(jì)4 邏輯設(shè)
發(fā)表于 10-25 16:29
?14次下載
數(shù)據(jù)庫學(xué)習(xí)入門資料之數(shù)據(jù)庫的概念結(jié)構(gòu)詳細(xì)資料概述
什么是概念結(jié)構(gòu)設(shè)計(jì)
將需求分析得到的用戶需求抽象為信息結(jié)構(gòu)即概念模型的過程就是概念結(jié)構(gòu)設(shè)計(jì)
概念結(jié)構(gòu)是各種數(shù)據(jù)模型的共同基礎(chǔ),它比
發(fā)表于 10-25 16:29
?0次下載
數(shù)據(jù)庫概念結(jié)構(gòu)是如何設(shè)計(jì)的概念結(jié)構(gòu)設(shè)計(jì)資料概述
本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)據(jù)庫概念結(jié)構(gòu)是如何設(shè)計(jì)的概念結(jié)構(gòu)設(shè)計(jì)資料概述主要內(nèi)容包括了:1 概念結(jié)構(gòu)2 概念結(jié)構(gòu)設(shè)計(jì)的方法與步驟3
發(fā)表于 10-26 11:49
?22次下載
數(shù)據(jù)庫的設(shè)計(jì)概念總結(jié)
本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)據(jù)庫的設(shè)計(jì)概念總結(jié)主要內(nèi)容包括了:1.數(shù)據(jù)庫設(shè)計(jì)概述,2.需求分析,3.概念結(jié)構(gòu)設(shè)計(jì),4.邏輯結(jié)構(gòu)設(shè)計(jì),5.數(shù)據(jù)庫
發(fā)表于 01-09 17:29
?13次下載
數(shù)據(jù)庫設(shè)計(jì)開發(fā)案例教程之數(shù)據(jù)庫設(shè)計(jì)的資料介紹
本文檔的主要內(nèi)容詳細(xì)介紹的是數(shù)據(jù)庫設(shè)計(jì)開發(fā)案例教程之數(shù)據(jù)庫設(shè)計(jì)的資料介紹主要內(nèi)容包括了:1 數(shù)據(jù)庫設(shè)計(jì)概述,2 需求分析,3 概念結(jié)構(gòu)設(shè)計(jì),4 邏輯
發(fā)表于 01-11 11:20
?17次下載
Qt學(xué)習(xí)筆記之數(shù)據(jù)庫結(jié)構(gòu)設(shè)計(jì)1
數(shù)據(jù)庫也是應(yīng)用程序的重要部分,一個完整的應(yīng)用程序幾乎都包含數(shù)據(jù)庫。
當(dāng)前主流的數(shù)據(jù)庫有DB2、MYSQL、OCI、ODBC、SQLITE、TDS、Oracle。
Sqlite是一款輕型的
Qt學(xué)習(xí)筆記之數(shù)據(jù)庫結(jié)構(gòu)設(shè)計(jì)2
數(shù)據(jù)庫也是應(yīng)用程序的重要部分,一個完整的應(yīng)用程序幾乎都包含數(shù)據(jù)庫。
當(dāng)前主流的數(shù)據(jù)庫有DB2、MYSQL、OCI、ODBC、SQLITE、TDS、Oracle。
Sqlite是一款輕型的
Qt學(xué)習(xí)筆記之數(shù)據(jù)庫結(jié)構(gòu)設(shè)計(jì)5
在Qt中,Qt為SQL數(shù)據(jù)庫提供支持的基本模塊。Qt SQL的API分為不同的層:
·驅(qū)動層
·SQL API層
·用戶接口層
Qt學(xué)習(xí)筆記之數(shù)據(jù)庫結(jié)構(gòu)設(shè)計(jì)6
在Qt中,Qt為SQL數(shù)據(jù)庫提供支持的基本模塊。Qt SQL的API分為不同的層:
·驅(qū)動層
·SQL API層
·用戶接口層
評論