Link to home
Start Free TrialLog in
Avatar of krush_ice
krush_ice

asked on

Excel automation

How to copy a cell or group of cells and paste in same sheet and in different sheets, how to copy a excel sheet
ASKER CERTIFIED SOLUTION
Avatar of kevinnguyen
kevinnguyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aerowang
aerowang

First you can create a new file with at "C:\\Book1.xls",and you can input a string at cell(a1),then you can establish a MFC project(dialog based),finally you can insert a button in the "about" dialog,
the code is as follows:
void CExcelDialog::OnButton1()
{              _Application2000 app;  // app is the Excel _Application object
      Workbooks2000 books;
      _Workbook2000 book;

      Worksheets2000 sheets;
      _Worksheet2000 sheet;
      Range2000 range;
      Font2000 font;
      Range2000 rows;
      Range2000 cols;
      Range2000 row;
      Range2000 col;
      Range2000 cells;
      Range2000 cell;

      VARIANT vtValue;
      VariantInit(&vtValue);
      COleVariant VOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
      
      // Start Excel and get Application object...
      // Instantiate Excel and open an existing workbook.
      if(!app.CreateDispatch("Excel.Application"))
      {
            AfxMessageBox("Couldn't CreateDispatch() for Excel");
            return;
      }

      books = app.GetWorkbooks();
      book = books.Open("C:\\Book1.xls",
            VOptional, VOptional, VOptional, VOptional,
            VOptional, VOptional, VOptional, VOptional,
            VOptional, VOptional, VOptional, VOptional);
      sheets =book.GetSheets();
      sheet = sheets.GetItem(COleVariant((short)1));

      range = sheet.GetRange(COleVariant("a1"),COleVariant("a1"));

      int nCount = range.GetCount();
      vtValue = range.GetValue();
      

      COleSafeArray sa;
      if( nCount > 1 )
            sa.Attach(vtValue);
      else{
            sa.CreateOneDim(VT_VARIANT, 1, &vtValue);
      }

      
      sheet = sheets.GetItem(COleVariant((short)2));
      range = sheet.GetRange(COleVariant("a1"),COleVariant("a1"));
                range.SetValue(COleVariant(sa));

      //Make the application visible and give the user control of Microsoft Excel
      app.SetVisible(TRUE);
      app.SetUserControl(TRUE);
      app.ReleaseDispatch();
}
Wish you good luck!
This program copy the string in cell(A1) of sheet1 to cell(A1) of sheet2,you can modify it to get more effects!
I'm using excel Automation with MFC these days.If you have some experience,shall we exchange?
I used to use MFC wrapper classes a lot for Excel automation. It simplies automation coding but adds a lot of overheads to executable file (you compare the size of exectable file created with MFC with that of executable file created with COM smart pointers).

If you know OLE automation is built on top of COM and you understand how COM works, you will find that you are better off using smart pointers (#import statement) rather than MFC wrapper classes.