当前位置:网站首页>QT excel operation summary

QT excel operation summary

2022-04-23 18:19:00 Things will turn when they reach the extreme 1024

1. Basic read and write file operation
2. The save dialog box pops up when saving
3. The problem of not saving the file after canceling the save dialog box

Reading and writing Excel

1.pro File to add

CONFIG += qaxcontainer

2. The header file

#include <ActiveQt/QAxObject>

3. Read file flow

Establish process and open file , Get table properties

// establish excel Process and get the total number of columns in the table 
    QAxObject excel("Excel.Application");     // Build process 
    excel.setProperty("Visible",false);    // I do not know! 
    QAxObject *workbooks = excel.querySubObject("WorkBooks");  // The following operations are all in here 
    QString initFile=QDir::currentPath();   // File current path 
    initFile+="/debug/Table.xlsx";
  //initFile+="/Table.xlsx";
    workbooks->dynamicCall("Open (const QString&)",initFile);    // Open file 
    QAxObject *workbook = excel.querySubObject("ActiveWorkBook");// Get active workbook 
    QAxObject *worksheet=workbook->querySubObject("WorkSheets(int)",1);// The first worksheet 
    QAxObject *used_range = worksheet->querySubObject("UsedRange");
    QAxObject *rows = used_range->querySubObject("Rows");    
    QAxObject *columns = used_range->querySubObject("Columns");
    int row_start = used_range->property("Row").toInt();  // Get the starting line 
    int column_start = used_range->property("Column").toInt();  // Get the starting column 
    int row_count = rows->property("Count").toInt();  // Get the number of lines 
    int column_count = columns->property("Count").toInt();  // Get the number of columns 
    qDebug()<<" Start line :"<<row_start<<" Start column :"<<column_start<<" Row number :"<<row_count<<" Number of columns :"<<column_count;

Read the file

QAxObject *rangeData = worksheet->querySubObject("Cells(int,int)",row,1); // obtain cell Value    row,1 Is the coordinate 
QString DataVal = rangeData->dynamicCall("Value2()").toString();   // Format 
Data[row-1]=DataVal;   // Take out 

Close document end excel Threads

// Close document end excel Threads 
    workbooks->dynamicCall("Close(Boolean)",false);           // close document 
    excel.dynamicCall("Quit(void)");                            // sign out excel Threads 
    //delete excel; // If you create excel When using a pointer, you need to delete the pointer   QAxObject *excel = new QAxObject(this);

4. Rewrite the file

Establish process and open file , Get table attributes as above

Rewrite the file

QAxObject *cell = worksheet->querySubObject("Cells(int,int)", row, 1);
cell->setProperty("Value", Data[row-1]);  // Set cell values 

Save the file

excel.setProperty("DisplayAlerts", false);/* Do not display any warning messages , If true, Then when it is closed, something like “ Whether to save the changes to XX file ”*/
workbook->dynamicCall("Save()");  // Save the active workbook file    This sentence can't be wrong 

// Close document end excel Threads 
workbooks->dynamicCall("Close(Boolean)",false);           // close document 
excel.dynamicCall("Quit(void)");                            // sign out excel Threads 

5. Summary routine

void MainWindow::writeToFile(QString *Data,QString *name,int len)
{
  // establish excel Process and get the total number of columns in the table 
    QAxObject excel("Excel.Application");
    excel.setProperty("Visible",false);
    QAxObject *workbooks = excel.querySubObject("WorkBooks");
    QString initFile=QDir::currentPath();
    initFile+="/debug/Table.xlsx";
  //initFile+="/Table.xlsx";
    workbooks->dynamicCall("Open (const QString&)",initFile);
    QAxObject *workbook = excel.querySubObject("ActiveWorkBook");// Get active workbook 
    QAxObject *worksheet=workbook->querySubObject("WorkSheets(int)",1);// The first worksheet 
    int row_count=Data->length();
     // Read and extract the contents of the form into the program 
    for(int row=1;row<row_count+1;row++)
      {
        QAxObject *cell = worksheet->querySubObject("Cells(int,int)", row, 1);
        cell->setProperty("Value", Data[row-1]);  // Set cell values 
        cell = worksheet->querySubObject("Cells(int,int)", row, 2);
        cell->setProperty("Value", name[row-1]);  // Set cell values 
        qDebug()<<row<<Data[row-1]<<"  "<<name[row-1];
      }
    excel.setProperty("DisplayAlerts", false);/* Do not display any warning messages , If true, Then when it is closed, something like “ Whether to save the changes to XX file ”*/
    workbook->dynamicCall("Save()");  // Save the active workbook file 
    // Close document end excel Threads 
    workbooks->dynamicCall("Close(Boolean)",false);           // close document 
    excel.dynamicCall("Quit(void)");                            // sign out excel Threads 

}
void MainWindow::readfromFile(QString *Data,QString *name,int len)
{
  // establish excel Process and get the total number of columns in the table 
    QAxObject excel("Excel.Application");
    excel.setProperty("Visible",false);
    QAxObject *workbooks = excel.querySubObject("WorkBooks");
    QString initFile=QDir::currentPath();
    initFile+="/debug/Table.xlsx";
  //initFile+="/Table.xlsx";
    workbooks->dynamicCall("Open (const QString&)",initFile);
    QAxObject *workbook = excel.querySubObject("ActiveWorkBook");// Get active workbook 
    QAxObject *worksheet=workbook->querySubObject("WorkSheets(int)",1);// The first worksheet 
    QAxObject *used_range = worksheet->querySubObject("UsedRange");
    QAxObject *rows = used_range->querySubObject("Rows");
    QAxObject *columns = used_range->querySubObject("Columns");
    int row_start = used_range->property("Row").toInt();  // Get the starting line 
    int column_start = used_range->property("Column").toInt();  // Get the starting column 
    int row_count = rows->property("Count").toInt();  // Get the number of lines 
    int column_count = columns->property("Count").toInt();  // Get the number of columns 
    qDebug()<<" Start line :"<<row_start<<" Start column :"<<column_start<<" Row number :"<<row_count<<" Number of columns :"<<column_count;
   // Redefine the cache array size according to the table size 
    Data->resize(row_count);
    name->resize(row_count);
     // Read and extract the contents of the form into the program 
    for(int row=row_start;row<row_count+1;row++)
      {
        QAxObject *rangeData = worksheet->querySubObject("Cells(int,int)",row,1); // obtain cell Value 
        QAxObject *rangeName = worksheet->querySubObject("Cells(int,int)",row,2); // obtain cell Value 
        QString DataVal = rangeData->dynamicCall("Value2()").toString();
        QString NameVal = rangeName->dynamicCall("Value2()").toString();
        Data[row-1]=DataVal;
        name[row-1]=NameVal;
        qDebug()<<row<<Data[row-1]<<"  "<<name[row-1];
        delete rangeData;delete rangeName;
      }
    // Close document end excel Threads 
    workbooks->dynamicCall("Close(Boolean)",false);           // close document 
    excel.dynamicCall("Quit(void)");                            // sign out excel Threads 
}

版权声明
本文为[Things will turn when they reach the extreme 1024]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610057618.html