电竞比分网-中国电竞赛事及体育赛事平台

分享

Qt創(chuàng)建excel示例

 logicsoft 2023-02-10 發(fā)布于浙江

用Qt5寫了個(gè)生成excel數(shù)據(jù)的程序,發(fā)現(xiàn)生成數(shù)據(jù)速度很慢,
經(jīng)查證,
在單元格很多的時(shí)候,按單元格寫入很慢,按范圍批量讀寫速度快很多,excel部分代碼片段如下,
初學(xué)者代碼不好,另外設(shè)置頁面和單元格格式有更好具體意見建議者,歡迎留言指導(dǎo)

這段代碼生成同一種格式的7頁數(shù)據(jù),最終結(jié)果如下:

void mainwindow::produce_excel()    
{
    //獲取桌面路徑,設(shè)置為表格的絕對(duì)路徑
    QString excel_file_path = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)
                             + "/baodi.xlsx";  
    //把"/baodi.xlsx"中//替換成所在系統(tǒng)分隔符,否則路徑讀取會(huì)失敗
    excel_file_path = QDir::toNativeSeparators(excel_file_path);    

    QAxObject *excel = new QAxObject(this);
    excel->setControl("Excel.Application"); //連接EXCEL控件
    excel->setProperty("DisplayAlerts", true);  //顯示窗體
    QAxObject *workbooks = excel->querySubObject("WorkBooks");  //  獲取工作?。‥XCEL文件)集合
    workbooks->dynamicCall("Add");  //創(chuàng)建新工作薄
    //workbooks->dynamicCall("Open(const QString&)", excel_file_path);
    QAxObject *workbook = excel->querySubObject("ActiveWorkBook");  //獲取當(dāng)前工作薄
    workbook->dynamicCall("SaveAs(const QString&, int, const QString&, const QString&, bool,bool)",
                          excel_file_path, 51, QString(""),QString(""),false,false);//51xlsx,56xls

    QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)", 1);
    //根據(jù)序號(hào)獲取EXCEL下方第int張工作表

/*
    //按單元格寫入,數(shù)據(jù)多時(shí),速度很慢,真的很慢,十分不推薦,已經(jīng)注釋掉了,按范圍寫入在后面
    QAxObject *usedRange = worksheet->querySubObject("UsedRange"); //sheet范圍
    int Row = usedRange->property("Row").toInt();// 獲得起始行數(shù)
    int Col = usedRange->property("Column").toInt();//獲得起始列數(shù)

    QAxObject *cell = worksheet->querySubObject("Cells(int,int)", Row, Col);
    const int ROW_NUM {30};
    const int COL_NUM {3};
    const int HIGHT {26};
    const int WIDE {10};
    long t = 0;//用于選擇時(shí)間time
    for(auto d:day)
    {
        //輸入表頭
        //auto t = time.begin();
        cell->setProperty("Value", d);
        cell->setProperty("RowHeight", HIGHT+9);//設(shè)置行高
        //cell->setProperty("ColumnWidth", WIDE-4);  //設(shè)置單元格列寬
        //cell->setProperty("HorizontalAlignment", -4108); 
        //左對(duì)齊(xlLeft):-4131  居中(xlCenter):-4108  右對(duì)齊(xlRight):-4152

        cell = worksheet->querySubObject("Cells(int,int)", ++Row, Col);
        cell->setProperty("Value", sn);
        cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE-4);  //設(shè)置單元格列寬
        cell->setProperty("HorizontalAlignment", -4108); 

        cell = worksheet->querySubObject("Cells(int,int)", Row, ++Col);
        cell->setProperty("Value", time[t++%6]);
        //cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬

        cell = worksheet->querySubObject("Cells(int,int)", Row, ++Col);
        //cell->setProperty("Value", sn);
        //cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬

        cell = worksheet->querySubObject("Cells(int,int)", Row, ++Col);
        cell->setProperty("Value", sn);
        //cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE-4);  //設(shè)置單元格列寬
        cell->setProperty("HorizontalAlignment", -4108); 

        cell = worksheet->querySubObject("Cells(int,int)", Row, ++Col);
        cell->setProperty("Value", time[t++%6]);
        //cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬
        cell->setProperty("HorizontalAlignment", -4108);

        cell = worksheet->querySubObject("Cells(int,int)", Row, ++Col);
        //cell->setProperty("Value", sn);
        //cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬

        cell = worksheet->querySubObject("Cells(int,int)", Row, ++Col);
        cell->setProperty("Value", sn);
        //cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE-4);  //設(shè)置單元格列寬
        cell->setProperty("HorizontalAlignment", -4108); 

        cell = worksheet->querySubObject("Cells(int,int)", Row, ++Col);
        cell->setProperty("Value", time[t++%6]);
        //cell->setProperty("RowHeight", HIGHT);
        cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬
        cell->setProperty("HorizontalAlignment", -4108); 

        Col = usedRange->property("Column").toInt();//獲得起始列數(shù)
        cell = worksheet->querySubObject("Cells(int,int)", ++Row, Col);
        //輸入數(shù)據(jù)
        for(int i=0; i<ROW_NUM; i++)
        {
            for(int j=0; j<COL_NUM; j++)
            {
                cell = worksheet->querySubObject("Cells(int,int)", Row, Col++);
                cell->setProperty("Value", i+1);
                cell->setProperty("RowHeight", HIGHT);
                //cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬
                cell->setProperty("HorizontalAlignment", -4108); 

                cell = worksheet->querySubObject("Cells(int,int)", Row, Col++);
                cell->setProperty("Value", rand());
                //cell->setProperty("RowHeight", HIGHT);
               // cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬
                cell->setProperty("HorizontalAlignment", -4108); 

                cell = worksheet->querySubObject("Cells(int,int)", Row, Col++);
                cell->setProperty("Value", right_model(model));
                //cell->setProperty("RowHeight", HIGHT);
                //cell->setProperty("ColumnWidth", WIDE);  //設(shè)置單元格列寬
                cell->setProperty("HorizontalAlignment", -4108); 

            }
            Col = usedRange->property("Column").toInt();//獲得起始列數(shù)
            cell = worksheet->querySubObject("Cells(int,int)", ++Row, Col);
        }



    }
*/


    //按范圍寫入,因?yàn)椴挥弥貜?fù)調(diào)用QAxObject,比按單元格寫入的方式速度提升巨大
    const int HIGHT{26};
    const int ROW_NUM {224};
    const int COL_NUM {3};
    unsigned int d = 0;
    unsigned int t = 0;

    //以二維數(shù)組的形式存儲(chǔ)預(yù)寫入數(shù)據(jù)
    QList<QList<QVariant>>datas;
    for(int i=0;i<ROW_NUM;i++)
    {

        QList<QVariant> rows;
        switch (i%32)//確定是第幾行
        {
            case 0 :
                rows.append(day[d++]);
                for(int k=0;k<COL_NUM*3-1;k++)
                {

                    //該方法必須輸入整個(gè)矩形區(qū)域,空的地方不輸入最后寫入結(jié)果會(huì)異常
                    rows.append("");

                }
            break; //  確定是第幾篇報(bào)文
            case 1 :
                for(int k=0;k<COL_NUM;k++)
                {

                    rows.append(sn);
                    rows.append(time[t++%6]);
                    rows.append("");

                }
            break;


            default:
                for(int k=0;k<COL_NUM;k++)
                {
                    rows.append(i%32?  i%32-1 : 30);
                    rows.append(rand());
                    rows.append(right_model(model));
                }
            break;
        }
        datas.append(rows);

    }

    /*
     *QVariant封裝絕大多數(shù)Qt提供的數(shù)據(jù)類型,只要放入和取出類型對(duì)應(yīng)即可,
     * 相當(dāng)于一個(gè)普遍的類型聯(lián)合,
     * canConvert可以查詢是否能轉(zhuǎn)換當(dāng)前類型,轉(zhuǎn)換類型以toT()命名
     * 以下為類型list<list<qvariant>>到qvariant的轉(zhuǎn)換過程
     
     * 待寫入?yún)^(qū)域內(nèi),每行存為一個(gè)QList<QVariant>,         
     * tjgcQList<QVariant> row1,row2,row3;
     
     * 將QList<QVariant> 轉(zhuǎn)換為QVariant類型,
     * QVariant r1(row1),r2(row2),r3(row3);
     
     * 整個(gè)寫入?yún)^(qū)域當(dāng)作一個(gè)QList<QVariant>,存入上述QVariant類型r1,r2,r3
     * 得到QList<QVariant> r
     
     * 整個(gè)寫入?yún)^(qū)域從QList<QVariant>轉(zhuǎn)換為QVariant類型
     * QVariant v(r); *
    */


    //二維數(shù)組轉(zhuǎn)一維
    QList<QVariant> vars;

    for(auto v:datas)
    {
        vars.append(QVariant(v));
    }
    //一維數(shù)組轉(zhuǎn)變量
    QVariant var = QVariant(vars);


    QAxObject *user_range = worksheet->querySubObject("Range(const QString&)", "A1:I224");//指定范圍
    user_range->setProperty("Value", var);//調(diào)用一次QAxObject即可完成寫入
    user_range->setProperty("RowHeight", HIGHT);//設(shè)置行高
    user_range->setProperty("HorizontalAlignment", -4108); 
    //左對(duì)齊(xlLeft):-4131  居中(xlCenter):-4108  右對(duì)齊(xlRight):-4152



    workbook->dynamicCall("Save()");    //保存文件
    workbook->dynamicCall("Close(Boolean)", false);
    excel->dynamicCall("Quit(void)");  //EXE結(jié)束前需要關(guān)閉EXCEL
    delete excel;

    //wait->close();
    //delete wait;
    QMessageBox::information(this,tr("注意"),QStringLiteral("報(bào)底已保存在桌面"),QMessageBox::Ok);



}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多