您好,登錄后才能下訂單哦!
本篇內容主要講解“Qt如何實現數據查詢導出打印”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Qt如何實現數據查詢導出打印”吧!
void DataHelper::dataout(const QString &fileName, const QString &sheetName, const QString &title, const QList<QString> &columnNames, const QList<int> &columnWidths, const QStringList &content, bool landscape) { DataContent dataContent; dataContent.fileName = fileName; dataContent.sheetName = sheetName; dataContent.title = title; dataContent.columnNames = columnNames; dataContent.columnWidths = columnWidths; dataContent.content = content; dataContent.landscape = landscape; dataout(dataContent); } QString DataHelper::dataout(QTableView *table, QStandardItemModel *model, quint8 type, const QString &file, const QString &title, const QString &sheet) { //從數據模型拿到行列 int row = model->rowCount(); int column = model->columnCount(); if (row == 0 || column == 0) { return QString(); } //獲取數據集合 QStringList content; for (int i = 0; i < row; ++i) { QStringList list; for (int j = 0; j < column; ++j) { list << model->item(i, j)->text(); } //每行數據作為一個整體字符串帶分割符 ; 存入 content << list.join(";"); } //獲取列名和列寬 QList<QString> columnNames; QList<int> columnWidths; for (int i = 0; i < column; ++i) { columnNames << model->headerData(i, Qt::Horizontal).toString(); columnWidths << table->columnWidth(i); } return dataout(type, file, title, sheet, content, columnNames, columnWidths); } QString DataHelper::dataout(QTableWidget *table, quint8 type, const QString &file, const QString &title, const QString &sheet) { //從數據模型拿到行列 int row = table->rowCount(); int column = table->columnCount(); if (row == 0 || column == 0) { return QString(); } //獲取數據集合 QStringList content; for (int i = 0; i < row; ++i) { QStringList list; for (int j = 0; j < column; ++j) { list << table->item(i, j)->text(); } //每行數據作為一個整體字符串帶分割符 ; 存入 content << list.join(";"); } //獲取列名和列寬 QList<QString> columnNames; QList<int> columnWidths; for (int i = 0; i < column; ++i) { columnNames << table->horizontalHeaderItem(i)->text(); columnWidths << table->columnWidth(i); } return dataout(type, file, title, sheet, content, columnNames, columnWidths); } QString DataHelper::dataout(quint8 type, const QString &file, const QString &title, const QString &sheet, const QStringList &content, const QStringList &columnNames, const QList<int> &columnWidths) { //設置結構體數據 DataContent dataContent; //填充內容 dataContent.content = content; //設置列名列寬 dataContent.columnNames = columnNames; dataContent.columnWidths = columnWidths; //設置標題 dataContent.title = title; //設置默認對齊 dataContent.defaultAlignment = 0; //如果是導出數據則先判斷文件名稱是否為空,為空則彈出文件對話框選擇 QString fileName = file; if (fileName.isEmpty() && type < 3) { //不同的格式后綴 QString filter = "all files (*.*)"; if (type == 0) { filter = "csv files (*.csv)"; } else if (type == 1) { filter = "xls files (*.xls)"; } else if (type == 2) { filter = "pdf files (*.pdf)"; } fileName = QFileDialog::getSaveFileName(0, "選擇保存文件", "", filter); if (fileName.isEmpty()) { return fileName; } } //設置文件名 dataContent.fileName = fileName; //xls還需要設置表名 dataContent.sheetName = sheet; //調用靜態函數 if (type == 0) { //重新組織內容,前面寫入標題,分隔符換成csv的定義的分隔符 QStringList list; //把標題加到內容中 list << columnNames.join(DataCsv::CsvSpliter); //重新更換分隔符 foreach (QString text, content) { text.replace(";", DataCsv::CsvSpliter); list << text; } DataCsv::outputData(fileName, list); } else if (type == 1) { DataXls::saveXls(dataContent); } else if (type == 2) { DataPrint::savePdf(dataContent); } else if (type == 3) { DataPrint::print(dataContent); } return fileName; }
到此,相信大家對“Qt如何實現數據查詢導出打印”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。