QXlsx icon indicating copy to clipboard operation
QXlsx copied to clipboard

large memery usage when export data from MYSQL to excel

Open wuyuewei opened this issue 9 months ago • 0 comments

The mysql database data size is 40909 rows and 50 colums, when use the code to export these data as blew the computer memery will be reached to 800Mb. the real excel file size only 7Mb. could anyone told me how to decrease the memery usage? ` QXlsx::Document xlsx; QXlsx::Format format; QModelIndex indexItem; QVariant data ;

    if(exportFileName.isEmpty())
        return;
    QFile file(exportFileName);;
    if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
    {
        MsgBox *msgBox = new MsgBox(Msg_Warning,exportFileName + "\n" + tr("The file is opend,please close file!"));
        msgBox->exec();
        delete msgBox;
        return;
    }
    else
    {  
        int rowCount = modelCrm->rowCount();
        int colCount = modelCrm->columnCount();
        ui->progressBar->setMaximum(rowCount);

        for (int i = 0; i < rowCount;i++)
        {
            QCoreApplication::processEvents();
            for(int j = 0; j < colCount; j++)
            {
                indexItem = modelCrm->index(i,j,QModelIndex());//rowNum,columnNum为行列号
                data = indexItem.data();
                    xlsx.write(i+3,j+1, data.toString());
            }
        }
    }
    QFuture<void> future = QtConcurrent::run([&]() {
        qDebug() << __FUNCTION__ << QThread::currentThreadId() << QThread::currentThread();
        xlsx.saveAs(exportFileName);
    });
    ui->progressBar->setRange(0, 0);
    // Set the progress bar to an indeterminate state
    while (!future.isFinished()) {
        QApplication::processEvents(QEventLoop::AllEvents, 10);
    } `

wuyuewei avatar Sep 05 '23 07:09 wuyuewei