A simple implementation of QT export Excel

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.