2: The header file contains # include
3: Define the QAxObjec object and specify the parent object.
4. Connect the controls. There are usually two kinds of controls, excel and wps, most of which are excel, but you should pay attention to whether the computer is.
Excel is installed and whether Excel is activated or not, because both cases will lead to reading failure.
Example:
//Excel is not installed on the user's computer, and the return value of SetControl is zero.
//The second if statement is to judge whether the user has installed wps, and if so, open the wps control.
If (! excel.setControl("Excel。 Application "))
{
If (! excel.setControl("ket。 Application ")? //Connect the Excel control
{
QMessageBox::about(NULL, "prompt", "Please install excel or wps?" );
Return;
}
}
5. Set the properties of reading. Xlsx file object.
Example:
excel.setProperty("Visible ",true); //Settings. The xlsx file is visible.
6: Get Workbook Collection
Example:
? QAxObject * work _ books = excel . query subobject(" WorkBooks ");
7: Open ours. Xlsl file by calling a function in COM.
Example:
Work _ books-> dynamic call(" Open(const QString & amp; )"、QString(" D:/luohui Qing/Qt excel . xlsx "));
/* Call the function named Open in COM, passing in the second parameter as the formal parameter of Open */
8: Get the active workbook
Example:
QAxObject * work _ book = excel . query subobject(" active workbook "); //Get the active workbook
9: Get all worksheets
Example:
QAxObject * work _ sheets = work _ book-& gt; query subobject(" WorkSheets "); //Get all worksheets, as shown in the figure.
10: get the number of worksheets
Example:
int sheet _ count = work _ sheets-& gt; Attribute ("Count"). toInt(); ? //Get the number of worksheets
1 1. When the number of worksheets determined is greater than zero, we need to operate the table, such as:
1 1. 1: Get the form in our workbook.
Example:
QAxObject * work _ sheet = work _ book-& gt; querySubObject("Sheets(int)", 1);
/* Get the first table in the workbook */
1 1.2: Get a set of values in our table.
Example:
QAxObject * used range = work _ sheet-& gt; query subobject(" used range ");
QVariant var = used range-& gt; dynamic call(" value ");
1 1.3: Store the value we got in a QVariantList container.
Example:
QVariantList varrow contents = var . to list();
1 1.4: Use const integer variable to save the number of data.
1 1.5: Note that the storage order of data in the container is line by line.
12: After we finish. Xslx file, wps or excel should be closed to prevent process residue.
Example:
excel . dynamic call(" Quit(void)");