[Qt PC] Open the local table file and obtain all the data in it

Foreword
In fact, the functions implemented in this article are not all the functions that bloggers want to implement, but only a small part of the total functions. This is just to record the implementation method and prevent it from being forgotten later. for reference only.

1. Realization effect

The current implementation is to open a local table file, output the total number of rows and columns of the table after opening, and then print out the entire content of the table. The test form is as follows

test form
The current host computer page is as follows

PC page

Click "Select File" to select the local table file

Select a document

Or you can enter the file path directly. Then click to open the file, and the total number of rows and columns of the selected table can be output on the Qt side, and the entire content of the table can be printed out. The results are as follows

Output results

2. UI design

The UI only uses two simple controls, listed here.

  • QPushButton
    "Select file" and open file use this control.
  • QLineEdit
    The file path uses this control.

3. Programming

3.1 Select local table file

The slot function of "Select File" is

void Widget::on_selectfile_Button_clicked()
{
    
    
    QString fileName = QFileDialog::getOpenFileName(this,QStringLiteral("选择文件"),"F:",QStringLiteral("表格(*xls *xlsx *csv);"));

    // 将文件路径显示到UI控件
    ui->selectedfilepath_lineEdit->setText(fileName);
}

After selecting the file, the file path will be displayed to the controlselectedfilepath_lineEdit.

If you need to add a file type, you can refer to the following modifications.

QString fileName = QFileDialog::getOpenFileName(this,QStringLiteral("选择文件"),"F:",QStringLiteral("表格(*xls *xlsx *csv);;图片(*jpg *png);"));

3.2 Get the total number of rows and columns of the table

    QAxObject *excel = new QAxObject(this);
    excel->setControl("Excel.Application");
    excel->setProperty("Visible", false);    //显示窗体看效果,选择ture将会看到excel表格被打开
    excel->setProperty("DisplayAlerts", true);
    QAxObject *workbooks = excel->querySubObject("WorkBooks");   //获取工作簿(excel文件)集合
    QString str = ui->selectedfilepath_lineEdit->text();
    //打开选定的excel
    workbooks->dynamicCall("Open(const QString&)", str);
    QAxObject *workbook = excel->querySubObject("ActiveWorkBook");
    QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)",1);

    // 获取行列数
    QAxObject *usedRange = worksheet->querySubObject("UsedRange");   //获取表格中的数据范围
    QAxObject *rows = usedRange->querySubObject("Rows");
    getrow = rows->property("Count").toInt();  //获取行数
    QAxObject *column = usedRange->querySubObject("Columns");
    getcolumn = column->property("Count").toInt();  //获取列数
    qDebug("行数为:%d   列数为:%d\n",getrow,getcolumn);

It is worth noting that this program obtains the total number of rows and columns of the first worksheet. If you need to modify it, you can modify the parameters in this function.

QAxObject *worksheet = workbook->querySubObject("WorkSheets(int)",2);

3.3 Obtain and output table contents

    QVariant var = usedRange->dynamicCall("Value");   // 将所有的数据读取到QVariant容器中保存
    QList<QList<QVariant>> excel_list;   // 用于将QVariant转换为Qlist的二维数组
    QVariantList varRows=var.toList();
    if(varRows.isEmpty())
    {
    
    
        return;
    }

    const int row_count = varRows.size();
    QVariantList rowData;
    for(int i=0;i<row_count;++i)
    {
    
    
        rowData = varRows[i].toList();
        excel_list.push_back(rowData);
    }

    //打印excel数据
    for(int i = 0; i<row_count; i++)
    {
    
    
        QList<QVariant> curList = excel_list.at(i);
        int curRowCount = curList.size();
        for(int j = 0; j < curRowCount; j++)
        {
    
    
            qDebug() << curList.at(j).toString();
        }
    }

2.4 Manipulate cell contents

In the final analysis, operating tables still comes down to operating cells. Here is an operation method.

QString ExcelName = worksheet->querySubObject("Cells(int,int)",所在行数,所在列数)->dynamicCall("Value").toString();

4. Operation examples

The operation examples here are written according to the blogger's own needs. They are to find out the column index where the message ID and message data are located. The program is posted here for reference only.

    // 遍历出报文ID和数据所属列数
    // 默认第一列为时间,不需要遍历
    for (int i = 1;i <= getcolumn;i ++)
    {
    
    
        // 遍历第一行全部内容
        ExcelName = worksheet->querySubObject("Cells(int,int)",1,i)->dynamicCall("Value").toString();

        // 查找报文ID所在列
        if (ExcelName == "MAKE_CAN_ID(HEX)")
        {
    
    
            qDebug("报文ID所在列为:%d",i);
            messageIDColumn = i;
        }

        // 查找数据所在列
        if (ExcelName == "DATA(HEX)")
        {
    
    
            qDebug("报文ID所在列为:%d",i);
            dataColumn = i;

            // 通常数据所在列在报文ID后面,所以遍历到数据所在列后直接跳出for循环
            break;
        }
    }
    qDebug("报文ID所在列为:%d   数据所在列为:%d\n",messageIDColumn,dataColumn);

Guess you like

Origin blog.csdn.net/qq_45217381/article/details/133810982