Results 1 to 3 of 3

Thread: Copy sheet from an excel file to another one

  1. #1
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Copy sheet from an excel file to another one

    Hello,

    I would like to copy a sheet from an excel file to another excel file. I try this code but it doesn't work (see below). I have no problem with compilation, just it doesn't copy the sheet from aaa.xlsx to bbb.xlsx. If someone knows the answer, please let me know! I able to copy a sheet in the same excel file but not to another.

    QAxObject* excel1 = new QAxObject("Excel.Application", 0);
    QAxObject* workbooks1 = excel1->querySubObject( "Workbooks" );
    QAxObject* workbook1 = workbooks1->querySubObject( "Open(const QString&)", "C:/Users/xxx/aaa.xlsx");
    QAxObject* sheets1 = workbook1->querySubObject( "Worksheets" );

    QAxObject* excel2 = new QAxObject("Excel.Application", 0);
    QAxObject* workbooks2 = excel2->querySubObject( "Workbooks" );
    QAxObject* workbook2 = workbooks2->querySubObject( "Open(const QString&)", "C:/Users/xxx/bbb.xlsx");
    QAxObject* sheets2 = workbook2->querySubObject( "Worksheets" );
    QAxObject* sheet2 = sheets2->querySubObject( "Item( int )", 1 );

    QAxObject* worksheet_copy_after = sheets1->querySubObject("Item( int )", 1 );
    QVariant param1 = worksheet_copy_after->asVariant();
    sheet2->dynamicCall("Copy (const QVariant&)",param1);

    excel1->setProperty("DisplayAlerts", false);
    excel1->dynamicCall("Save()");

    workbook1->dynamicCall("Close (Boolean)", true);
    excel1->dynamicCall("Quit (void)");

    excel2->setProperty("DisplayAlerts", false);
    excel2->dynamicCall("Save()");

    workbook2->dynamicCall("Close (Boolean)", true);
    excel2->dynamicCall("Quit (void)");

    delete sheets1;
    delete workbook1;
    delete workbooks1;
    delete excel1;

    delete sheet2;
    delete sheets2;
    delete workbook2;
    delete workbooks2;
    delete excel2;

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Copy sheet from an excel file to another one

    You really need the Excel automation documentation not Qt advice. You are not copying a worksheet within a workbook but doing the equivalent of a copy and paste between workbooks. Is there no Paste() or Insert() operation to put the data in your variant into the second work book?

  3. #3
    Join Date
    May 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Copy sheet from an excel file to another one

    Thanks for your answer! I already tried Paste(), Copy(), Insert() but nothing happens! :-(
    By doing :

    1.QAxObject* worksheet_copy_after = sheets1->querySubObject("Item( int )", 1 );
    2.QVariant param1 = worksheet_copy_after->asVariant();
    3.worksheet_copy_after ->dynamicCall("Copy (const QVariant&)",param1);

    The "function" Copy() works but only using one excel file (copy the first worksheet named, for example "aaa", and insert it before "aaa" and named it "aaa(2)".
    When I replace "worksheet_copy_after" by a worksheet (sheet2) of another workbook, it does not work.
    If you have a link for excel automation using Qt, please let me know. If you have also some ideas, do not hesitate.

    Thanks again for your help and comments.

    Thanks for your answer! I already tried Paste(), Copy(), Insert() but nothing happens! :-(
    By doing :

    1.QAxObject* worksheet_copy_after = sheets1->querySubObject("Item( int )", 1 );
    2.QVariant param1 = worksheet_copy_after->asVariant();
    3.worksheet_copy_after ->dynamicCall("Copy (const QVariant&)",param1);

    The "function" Copy() works but only using one excel file (copy the first worksheet named, for example "aaa", and insert it before "aaa" and named it "aaa(2)".
    When I replace "worksheet_copy_after" by a worksheet (sheet2) of another workbook, it does not work.
    If you have a link for excel automation using Qt, please let me know. If you have also some ideas, do not hesitate.

    Thanks again for your help and comments.

Similar Threads

  1. copy table to Excel
    By Arend in forum Newbie
    Replies: 8
    Last Post: 27th February 2013, 06:32
  2. copy tableview to Excel
    By Arend in forum Newbie
    Replies: 3
    Last Post: 9th December 2012, 21:58
  3. copy/move excel sheet with QT
    By gerardpuducul in forum Qt Programming
    Replies: 16
    Last Post: 4th December 2012, 09:25
  4. ActiveQt hosting Excel sheet
    By ArmanS in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2010, 10:27
  5. How to copy from Excel and paste to QTableView
    By sms in forum Qt Programming
    Replies: 5
    Last Post: 7th February 2009, 03:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.