Skip to content
Jay Two edited this page Oct 2, 2019 · 28 revisions

QtXlsx wiki

  • If you would like to participate in the wiki, please submit an issue or proceed with a Pull Request. 😄

Examples

1️⃣ Basic reading data.

	QXlsx::Document xlsx("ReadExcel.xlsx");
	if (xlsx.load())
	{
		int row = 1; int col = 1;
		Cell* cell = xlsx.cellAt(row, col);
		if (cell != NULL)
		{
			QVariant readValue = cell->readValue();
			// Type of readValue is string, number, date/time, etc.
			// See QVariant::type() 
		}
	}

2️⃣ Basic writing data.

	QXlsx::Document xlsx;			
	QVariant writeValue = QString("hello");
	int row = 1; int col = 1;
	xlsx.write(row, col, writeValue);

3️⃣ Add sheet

	xlsx.addSheet("added sheet"); // current sheet is 'added sheet'.
	xlsx.write(2, 2, "hello"); 

4️⃣ Read sheet data

	xlsx.selectSheet("added sheet"); // current sheet is 'added sheet'.
	Cell* cell = xlsx.cellAt(row, col);
	if (cell != NULL)
	{
		QVariant readValue = cell->readValue();
	}	

5️⃣ Read all sheets data

	int sheetIndexNumber = 0;
	foreach( QString curretnSheetName, xlsxDoc.sheetNames() )
    {
		// get current sheet 
        QXlsx::AbstractSheet* currentSheet = xlsxDoc.sheet( curretnSheetName );
        if ( NULL == currentSheet )
            continue;

        // get full cells of current sheet
        int maxRow = -1;
        int maxCol = -1;
        currentSheet->workbook()->setActiveSheet( sheetIndexNumber );
        Worksheet* wsheet = (Worksheet*) currentSheet->workbook()->activeSheet();
        if ( NULL == wsheet )
            continue;

        QString strSheetName = wsheet->sheetName(); // sheet name
        qDebug() << strSheetName; 

        QVector<CellLocation> clList = wsheet->getFullCells( &maxRow, &maxCol );

        QVector< QVector<QString> > cellValues;
        for (int rc = 0; rc < maxRow; rc++)
        {
            QVector<QString> tempValue;
            for (int cc = 0; cc < maxCol; cc++)
            {
                tempValue.push_back(QString(""));
            }
            cellValues.push_back(tempValue);
        }

        for ( int ic = 0; ic < clList.size(); ++ic )
         {
            CellLocation cl = clList.at(ic); // cell location

            int row = cl.row - 1;
            int col = cl.col - 1;

            QSharedPointer<Cell> ptrCell = cl.cell; // cell pointer

            // value of cell
            QVariant var = cl.cell.data()->value();
            QString str = var.toString();

            cellValues[row][col] = str;
        }

        for (int rc = 0; rc < maxRow; rc++)
        {
            for (int cc = 0; cc < maxCol; cc++)
            {
                QString strCell = cellValues[rc][cc];
				qDebug() << "( row : " << rc << ", col : " << cc << ") "
				         << strCell; // display cell value
            }
        }

        sheetIndexNumber++;
    }
Clone this wiki locally