Qt Qaxobject operation Excel File Process summary (RPM):

Source: Internet
Author: User






Just the colleague asked Qt to operate Excel. Transferred from: http://blog.csdn.net/a156392343/article/details/48092515












Compounding aspects:






1, ensure that EXCEL software registered successfully in the local server, not registered successfully can be run in the "E:\program Files\Microsoft Office\office12\excel. EXE "/RegServer Manual registration, note the path to use your own Excel path.






2. Make sure that the components are properly configured, run the command: DCOMCNFG, see if there is a Microsoft Excel application under the DCOM configuration, and start setting up (1) general, authentication: None (2) Security, provisioning permissions customization, control permissions, Full Control selected, save






The above ensures that the qaxobject can be called normally






Procedural aspects:






1. Project Pro file add










CONFIG + = Qaxcontainer




2.. h file Additions
















      /1./Open


void Newexcel (const QString &filename);



      2. Increase of 1 worksheet


      void Appendsheet (const QString &sheetname,int cnt);



      3. Writing data to Excel cells


      void Setcellvalue (int row, int column, const QString &value);



      4. Save Excel


     void Saveexcel (const QString &filename);



      5. Release Excel


      void  freeexcel ();
















3. cpp files










1. Create a new Excel


void Mainwindow::newexcel (const QString &filename)


{        HRESULT r = oleinitialize (0);


        CoInitialize (0);


        if (r! = S_OK && r! = S_FALSE) {


            Qwarning ("Qt:could not initialize OLE (error%x) \ n", (unsigned int) r);


        }


        Papplication = new Qaxobject ("Excel.Application");


             if (papplication = = NULL) {


                 Qwarning ("papplication\n"); return;


             }


   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 Mainwindow::appendsheet (const QString &sheetname,int cnt)


{


   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 Mainwindow::setcellvalue (int row, int column, const QString &value)


{


   Qaxobject *prange = Psheet->querysubobject ("Cells (Int,int)", Row, column);


   Prange->dynamiccall ("value", value);


}


4. Save Excel


void Mainwindow::saveexcel (const QString &filename)


{


   Pworkbook->dynamiccall ("SaveAs (const QString &)",


   Qdir::tonativeseparators (FileName));


}


5. Release Excel


void  Mainwindow::freeexcel ()


{


   if (papplication! = NULL)


   {


       Papplication->dynamiccall ("Quit ()");


       Delete papplication;


       Papplication = NULL;


   }


}











Attach the test button slot function










void Mainwindow::on_pushbutton_keting_clicked ()


{



OleInitialize (0);



QString filenametmp = "D:/112.xls"; File names can be modified from here


   Newexcel (FILENAMETMP);


  Appendsheet ("Page1");


   Setcellvalue (3, 3, "34343");


   Setcellvalue (3, 6, "55555");


   Saveexcel (FILENAMETMP);


   Freeexcel ();


   Return


}










Qt qaxobject Operations Excel file Process summary (GO):




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.