PDA

View Full Version : Copy sheet from an excel file to another one



Snake2009
12th May 2015, 16:09
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;

ChrisW67
12th May 2015, 21:42
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?

Snake2009
12th May 2015, 22:25
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.