一、源码分享
bool ReportFormUtils::exportReportHtml(QString &errString, const QString tableName, const QString savePathAndName, const QString tableTitle, const QString tableInfo)
{
Q_UNUSED(errString)
Q_UNUSED(tableName)
Q_UNUSED(savePathAndName)
#define SINGLE_OPT_ROW 1000
QString cmd;
quint32 i,j;
quint32 row,col;
bool occerError = false;
QString fileWriteContent;
//判断文件是否存在
QFile file(savePathAndName);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
{
errString = tr("文件打开失败!");
return false;
}
QTextStream out(&file);
out << QString("<!DOCTYPE html>") << Qt::endl;
out << QString("<html>") << Qt::endl;
out << QString("<head>") << Qt::endl;
out << QString("<meta charset=\"utf-8\">") << Qt::endl;
out << QString("<title>");
out << tableTitle << QString("</title>") << Qt::endl;
out << QString("</head>") << Qt::endl;
out << QString("<body>") << Qt::endl;
out << QString(tableInfo) << Qt::endl;
out << QString("<table border=\"1\">") << Qt::endl;
SqliteHelper* sqlHelper = SqliteHelper::getInstance();
QSqlDatabase *db = sqlHelper->getDatabase();
QSqlQuery sqlQuery(*db);
QSqlRecord record;
//开始设置表头
cmd =QString("select * from %1").arg(tableName);
sqlQuery.prepare(cmd);
if(!sqlQuery.exec())
{
errString = tr("数据库表头获取失败!");
occerError = true;
goto exit;
}
record = sqlQuery.record();
col = record.count(); //获取列数
//qDebug()<<"col:"<<col;
//写入表头
out << QString("<tr>") << Qt::endl;
for(i=0;i<col;++i)
{
out << formatCell(record.fieldName(i)) << Qt::endl;
}
out << QString("</tr>") << Qt::endl;
//获取数据行数
cmd =QString("SELECT count(*) FROM %1;").arg(tableName);
sqlQuery.prepare(cmd);
if(!sqlQuery.exec())
{
errString = tr("数据库行数获取失败!");
occerError = true;
goto exit;
}
sqlQuery.next();
row = sqlQuery.value(0).toInt();
//开始导出数据
for(i =0;i < row; i+=SINGLE_OPT_ROW)
{
sqlQuery.clear();
cmd =QString("select * from %1 limit %2,%3").arg(tableName).arg(i).arg(SINGLE_OPT_ROW);
//qDebug()<<cmd;
sqlQuery.prepare(cmd);
if(!sqlQuery.exec())
{
errString = tr("数据库数据获取失败!");
occerError = true;
goto exit;
}
//读取数据
while(sqlQuery.next())
{
out << QString("<tr>") << Qt::endl;
for(j=0;j<col;j++)
{
out << formatCell(sqlQuery.value(j).toString()) << Qt::endl;
}
out << QString("</tr>") << Qt::endl;
}
}
out << QString("</table>") << Qt::endl;
out << QString("</body>") << Qt::endl;
out << QString("</html>") <<Qt::endl;
qDebug()<<"currentThreadId = "<<QThread::currentThreadId();
exit:
file.close();
db->close();
delete db;
if(occerError)
return false;
return true;
}
void ReportFormUtils::castListListVariant2Variant(const QList<QList<QVariant> > &cells, QVariant &res)
{
QVariantList vars;
const int rows = cells.size();
for(int i=0;i<rows;++i)
{
vars.append(QVariant(cells[i]));
}
res = QVariant(vars);
}
QString ReportFormUtils::formatCell(QString val)
{
QString outStr = "<td";
//文本对齐
outStr += " align=\"";
outStr += "center";
outStr += "\"";
//跨列
outStr += QString::asprintf(" colspan=\"%d\"", 1);
//跨行
outStr += QString::asprintf(" rowspan=\"%d\"", 1);
//背景色 bgcolor
outStr += QString::asprintf(" bgcolor=\"#%x\"", 0xcce8cf);
outStr += ">";
//内容
if (val.size()) {
outStr += val;
}
outStr += "</td>";
return outStr;
}
二、导出文件查看
三、浏览器查看