Qt将数据写入EXCEL

添加头文件

#include

在.pro文件中添加:

QT       += axcontainer

然后就是对EXCEL的操作

bool adminFinancial::exportToExcel()
{
    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,*cellD;

            //设置标题
            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);
            cellD=worksheet->querySubObject("Range(QVariant, QVariant)",D);
            cellA->dynamicCall("SetValue(const QVariant&)",QVariant("流水号"));//设置单元格的值
            cellB->dynamicCall("SetValue(const QVariant&)",QVariant("用户名"));
            cellC->dynamicCall("SetValue(const QVariant&)",QVariant("金额"));
            cellD->dynamicCall("SetValue(const QVariant&)",QVariant("日期"));
            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);
                cellD=worksheet->querySubObject("Range(QVariant, QVariant)",D);
                cellA->dynamicCall("SetValue(const QVariant&)",QVariant(this->model->item(i,0)->data(Qt::DisplayRole).toString()));//设置单元格的值
                cellB->dynamicCall("SetValue(const QVariant&)",QVariant(this->model->item(i,1)->data(Qt::DisplayRole).toString()));
                cellC->dynamicCall("SetValue(const QVariant&)",QVariant(this->model->item(i,2)->data(Qt::DisplayRole).toString()));
                cellD->dynamicCall("SetValue(const QVariant&)",QVariant(this->model->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;
        }
        return true;
}

 

 

你可能感兴趣的:(Qt从入门到放弃)