本篇内容主要讲解“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。