Qt之导出文件到excel

发现没有人逃得过导出文件导入文件,如果有,那就是时候未到

void MainWindow::on_pushButton_clicked()
{
    //获取保存路径
    QString filepath=QFileDialog::getSaveFileName(this,tr("Save"),".",tr(" (*.xlsx)"));
    if(!filepath.isEmpty()){
        QAxObject *excel = new QAxObject(this);
        //连接Excel控件
        excel->setControl("Excel.Application");
        //不显示窗体
        excel->dynamicCall("SetVisible (bool Visible)","false");
        //不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改,是否保存”的提示
        excel->setProperty("DisplayAlerts", false);
        //获取工作簿集合
        QAxObject *workbooks = excel->querySubObject("WorkBooks");
        //新建一个工作簿
        workbooks->dynamicCall("Add");
        //获取当前工作簿
        QAxObject *workbook = excel->querySubObject("ActiveWorkBook");
        //获取工作表集合
        QAxObject *worksheets = workbook->querySubObject("Sheets");
        //获取工作表集合的工作表1,即sheet1
        QAxObject *worksheet = worksheets->querySubObject("Item(int)",1);

        QAxObject *cellA,*cellB,*cellC,*cellD;
        //设置标题
        int cellrow=1;
        //设置要操作的单元格,如A1
        QString A="A"+QString::number(cellrow);
        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);
        cellD = worksheet->querySubObject("Range(QVariant, QVariant)",D);
        //设置单元格的表头值
        cellA->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->horizontalHeaderItem(0)->text()));
        cellB->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->horizontalHeaderItem(1)->text()));
        cellC->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->horizontalHeaderItem(2)->text()));
        cellD->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->horizontalHeaderItem(3)->text()));
        cellrow++;
        int rows = ui->tableWidget->rowCount();
        //设置表格数据
        for(int i=0;iquerySubObject("Range(QVariant, QVariant)",A);
            cellB = worksheet->querySubObject("Range(QVariant, QVariant)",B);
            cellC = worksheet->querySubObject("Range(QVariant, QVariant)",C);
            cellD = worksheet->querySubObject("Range(QVariant, QVariant)",D);
            //设置单元格的值
            cellA->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->item(i,0)->data(Qt::DisplayRole).toString()));
            cellB->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->item(i,1)->data(Qt::DisplayRole).toString()));
            cellC->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->item(i,2)->data(Qt::DisplayRole).toString()));
            cellD->dynamicCall("SetValue(const QVariant&)",QVariant(ui->tableWidget->item(i,3)->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;
        qDebug() << "\n导出成功啦!!!";
    }
}
当数据量过大时使用
        for(int i=1;i<=496;i++){
            //设置设置某行某列
            QAxObject *Range = worksheet->querySubObject("Cells(int,int)", i, i);
            Range->dynamicCall("SetValue(const QString &)",QString("ch:%1").arg(i));
        }

程序界面:

Qt之导出文件到excel_第1张图片

点击导出,看生成的excel表:

Qt之导出文件到excel_第2张图片

导出实验就做好啦

下载链接,不过没别的东西:https://download.csdn.net/download/qq_41399894/11424016

你可能感兴趣的:(Qt)