您的位置:首页 > 编程语言 > Qt开发

Read the sheets in the Excel with QT

2010-02-10 21:10 567 查看
 #include <QAxObject>

 

//open file
    QString fileName = QFileDialog::getOpenFileName(this,tr("Open Spreadsheet"), ".", tr("Spreadsheet files (*.xlsx)"));
    if (!fileName.isEmpty())
    {

        int r = QMessageBox::information(this, fileName.toLatin1().mid(fileName.toLatin1().lastIndexOf("/")+1,fileName.toLatin1().length()),
                                tr(fileName.toLatin1().mid(fileName.toLatin1().lastIndexOf("/")+1,fileName.toLatin1().length())+" "+"has been found./n"
                                   "Do you want to open this file?"),
                                QMessageBox::Yes | QMessageBox::Default,
                                QMessageBox::Cancel | QMessageBox::Escape);
        if(r==QMessageBox::Yes)
        {
          // load file into the excel ojbect

            QAxObject* excel = new QAxObject( "Excel.Application", 0 );
            QAxObject* workbooks = excel->querySubObject( "Workbooks" );
            QAxObject* workbook = workbooks->querySubObject( "Open(QString&)", fileName );
            QAxObject* sheets = workbook->querySubObject( "Worksheets" );

            QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row

            //worksheets count
            int count=sheets->dynamicCall("Count()").toInt();
            for(int i=1;i<count;i++)
            {
                //sheet pointer
                QAxObject* sheet = sheets->querySubObject( "Item( int )", i );

                QAxObject* rows = sheet->querySubObject( "Rows" );
                int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255, so you have to check somehow validity of cell values
                QAxObject* columns = sheet->querySubObject( "Columns" );
                int columnCount = columns->dynamicCall( "Count()" ).toInt(); //similarly, always returns 65535

                //One of possible ways to get column count
                int currentColumnCount = 0;
                for (int col=1; col<columnCount; col++)
                {
                        QAxObject* cell = sheet->querySubObject( "Cells( int, int )", 1, col );
                        QVariant value = cell->dynamicCall( "Value()" );
                        if (value.toString().isEmpty())
                                break;
                        else
                                currentColumnCount = col;
                }
                columnCount = currentColumnCount;

                //sheet->dynamicCall( "Calculate()" ); //maybe somewhen it's necessary, but i've found out that cell values are calculated without calling this function. maybe it must be called just to recalculate

                for (int row=1; row <= rowCount; row++)
                {
                        QVariantList dataRow;
                        bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course, it maybe not right for different excel files. it's just criteria to calculate somehow row count for my file)
                        for (int column=1; column <= columnCount; column++)
                        {
                                QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, column );
                                QVariant value = cell->dynamicCall( "Value()" );
                                if (!value.toString().isEmpty() && isEmpty)
                                        isEmpty = false;
                                dataRow.append(value);
                        }
                        if (isEmpty) //criteria to get out of cycle
                                break;
                        data.append(dataRow);
                }

            }

            QMessageBox::information(this,"test",data[0].value(0).toString(),QMessageBox::Yes | QMessageBox::Default);
            //QTableView *view=new QTableView();
            // QFile file("c:/test.txt");
            // char *msg="hello";
            // file.open(QIODevice::WriteOnly);
             //file.write(msg);        // write to stderr
             //file.close();
            workbook->dynamicCall("Close()");
            excel->dynamicCall("Quit()");
        }
        else
        {
         this->close();
        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐