QXlsx basic operation record

  • Open an Excel file

    QString filePath = QFileDialog::getSaveFileName(this, tr("选择保存路径"), tr("新建.xlsx").arg(modelName), QStringLiteral("*.xlsx"));
    if(filePath.isEmpty())
    {
        return;
    }
    QXlsx::Document xlsxDocument(filePath);
  • Cell style settings

    QXlsx::Format titleFormat;//标题单元格样式
    titleFormat.setFontSize(20);/*设置字体大小*/
    titleFormat.setFontBold(true);//字体加粗
    titleFormat.setHorizontalAlignment(QXlsx::Format::AlignHCenter);//横向居中
    titleFormat.setVerticalAlignment(QXlsx::Format::AlignVCenter);//纵向居中
    titleFormat.setBorderStyle(QXlsx::Format::BorderDashDotDot);//边框样式
  • Write cell

    xlsxDocument.write(1, 1, QString("测试"),titleFormat);
  • Merge cells, merge (3,1) to (3,3) into one cell

    xlsxDocument.mergeCells(QXlsx::CellRange(3,1,3,3), tableTitleFormat);
  • Save Excel file

    xlsxDocument.saveAs(filePath);
  • Get the number of rows and columns

    QXlsx::Document xlsx(filePath);
    QXlsx::Workbook *workBook = xlsx.workbook();
    QXlsx::Worksheet *workSheet = static_cast<QXlsx::Worksheet*>(workBook->sheet(0));

    int row = workSheet->dimension().rowCount();
    int col = workSheet->dimension().columnCount();
  • Traverse content

    for(int i = 1;i <= row;++i)
    {
        for(int j = 1;i <= col;++j)
        {
            if(QXlsx::Cell * cell = workSheet->cellAt(i, j))
            {
                qDebug()<< cell->value().toString();
            }
        }
    }
  • Select current worksheet and add worksheet
    if(!xlsx.selectSheet(bookName))
    {
        xlsx.addSheet(bookName);
    }
  • Rename worksheet
    xlsx.workbook()->renameSheet(0,workBookName);

 

Guess you like

Origin blog.csdn.net/kenfan1647/article/details/113862808