Qaxobject COM objects are encapsulated, Qaxobject derives from Qaxbase, while the latter provides a set of APIs to access COM objects directly through IUnknown (unclear IUnknown classmates can go to see COM object model) pointers. We're talking about Excel, which is also a COM object, so we can manipulate it through qaxobject, and for the sake of understanding, let's first look at the main hierarchies of Excel objects:
is a hierarchy of Excel objects, 1 Excel has 1 application objects, and 1 application objects consist of multiple workbook objects, which are managed uniformly by workbooks objects. Workbook object can contain several worksheet, these worksheet objects also have a worksheets object to be unified management, followed by a Range object, this object corresponds to the table cell in the worksheet, Well, everyone should know the main hierarchy of Excel objects, let's look at how Qaxobject is exporting Excel:
1. Create a new Excel
Qaxobject *papplication = NULL;
Qaxobject *pworkbooks = NULL;
Qaxobject *pworkbook = NULL;
Qaxobject *psheets = NULL;
Qaxobject *psheet = NULL;
void Newexcel (const QString &filename)
{
Papplication = new Qaxobject ();
Papplication->setcontrol ("Excel.Application");//Connect Excel control
Papplication->dynamiccall ("setvisible (BOOL)", false),//false does not display the form
Papplication->setproperty ("DisplayAlerts", false);//Do not display any warning messages.
Pworkbooks = Papplication->querysubobject ("Workbooks");
QFile file (fileName);
if (File.exists ())
{
Pworkbook = Pworkbooks->querysubobject ("Open (const QString &)", FileName);
}
Else
{
Pworkbooks->dynamiccall ("Add");
Pworkbook = Papplication->querysubobject ("ActiveWorkbook");
}
Psheets = Pworkbook->querysubobject ("Sheets");
Psheet = Psheets->querysubobject ("Item (int)", 1);
}
2. Increase of 1 worksheet
void Appendsheet (const QString &sheetname)
{
Qaxobject *plastsheet = Psheets->querysubobject ("Item (int)", CNT);
Psheets->querysubobject ("Add (qvariant)", Plastsheet->asvariant ());
Psheet = Psheets->querysubobject ("Item (int)", CNT);
Plastsheet->dynamiccall ("Move (qvariant)", Psheet->asvariant ());
Psheet->setproperty ("Name", sheetname);
}
3. Writing data to Excel cells
void Setcellvalue (int row, int column, const QString &value)
{
Qaxobject *prange = Psheet->querysubobject ("Cells (Int,int)", Row, column);
Range->dynamiccall ("value", value);
}
4. Save Excel
void Saveexcel (constqstring &filename)
{
Pworkbook->dynamiccall ("SaveAs (const QString &)",
Qdir::tonativeseparators (FileName));
}
5. Release Excel
void Freeexcel ()
{
if (papplication! = NULL)
{
Papplication->dynamiccall ("Quit ()");
Delete papplication;
Papplication = NULL;
}
}
http://blog.csdn.net/rabinsong/article/details/8571021
A simple implementation of QT export Excel