qt sqlite导出excel

void Result_num::on_pushButton_output_clicked()
{
    QSqlDatabase mydb=QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("./caminfo.db");
    mydb.open();
    model = new QSqlTableModel(this);
    model->setTable("Result");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select(); //选取整个表的所有行

    QString filepath=QFileDialog::getSaveFileName(this,tr("Save orbit"),".",tr("Microsoft Office 2007 (*.xlsx)"));//获取保存路径
            if(!filepath.isEmpty()){
                QAxObject *excel = new QAxObject(this);
                excel->setControl("Excel.Application");//连接Excel控件
                excel->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
                excel->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示

                QAxObject *workbooks = excel->querySubObject("WorkBooks");//获取工作簿集合
                workbooks->dynamicCall("Add");//新建一个工作簿
                QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//获取当前工作簿
                QAxObject *worksheets = workbook->querySubObject("Sheets");//获取工作表集合
                QAxObject *worksheet = worksheets->querySubObject("Item(int)",1);//获取工作表集合的工作表1,即sheet1

                QAxObject *cellA,*cellB,*cellC;

                //设置标题
                int cellrow=1;
                QString A="A"+QString::number(cellrow);//设置要操作的单元格,如A1
                QString B="B"+QString::number(cellrow);
                QString C="C"+QString::number(cellrow);
                QString D="D"+QString::number(cellrow);
                cellA = worksheet->querySubObject("Range(QVariant, QVariant)",A);//获取单元格
                cellB = worksheet->querySubObject("Range(QVariant, QVariant)",B);
                cellC=worksheet->querySubObject("Range(QVariant, QVariant)",C);
                cellA->dynamicCall("SetValue(const QVariant&)",QVariant("time"));//设置单元格的值
                cellB->dynamicCall("SetValue(const QVariant&)",QVariant("url"));
                cellC->dynamicCall("SetValue(const QVariant&)",QVariant("numberPeople"));
                cellrow++;

                int rows=this->model->rowCount();
                for(int i=0;iquerySubObject("Range(QVariant, QVariant)",A);//获取单元格
                    cellB = worksheet->querySubObject("Range(QVariant, QVariant)",B);
                    cellC=worksheet->querySubObject("Range(QVariant, QVariant)",C);
                    cellA->dynamicCall("SetValue(const QVariant&)",QVariant(this->model->index(i,0).data(Qt::DisplayRole).toString()));//设置单元格的值
                    cellB->dynamicCall("SetValue(const QVariant&)",QVariant(this->model->index(i,1).data(Qt::DisplayRole).toString()));
                    cellC->dynamicCall("SetValue(const QVariant&)",QVariant(this->model->index(i,2).data(Qt::DisplayRole).toString()));
                    cellrow++;
                }

                workbook->dynamicCall("SaveAs(const QString&)",QDir::toNativeSeparators(filepath));//保存至filepath,注意一定要用QDir::toNativeSeparators将路径中的"/"转换为"\",不然一定保存不了。
                workbook->dynamicCall("Close()");//关闭工作簿
                excel->dynamicCall("Quit()");//关闭excel
                delete excel;
                excel=NULL;
            }
            mydb.close();
}

 

代码如上,很简单,但是导出时间较长qt sqlite导出excel_第1张图片

你可能感兴趣的:(qt sqlite导出excel)