Solved

Accessing Excel in C++ Builder 5.0

Posted on 2003-12-11
11
2,231 Views
Last Modified: 2007-12-19
Hello All

 I hope you can help. I am developing an application in C++ builder 5.0 that requires non-linear curve fitting. The excel solve function performs this operation. So from my application I want to puts numbers into an excel spreadsheet, run the solves function and read the results back out into my application. I have been looking at some of the stuff written here, and being relativity new to all this OLE, DDE and Automation stuff am finding it a bit confusing. Again hope you can help!
0
Comment
Question by:JFoster4743
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 6

Expert Comment

by:Triskelion
ID: 9944560
Does C++ builder allow you to import .OCX or .OLB files?
If so, check out this automation page on the Microsoft support site:
   http://support.microsoft.com/default.aspx?scid=kb;en-us;179706
0
 

Author Comment

by:JFoster4743
ID: 9956335
I believe it does. But the pages listed require building things in Microsofts Developer and VC. I don't have eather of these. I have been doing a lot of research and am begining to think this is an imposible task. Thanks anyway.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 9961305
I understand, but if it will import the OCX and OLB files, you can simply use the page previously listed as an idea of how to expose the interface.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JFoster4743
ID: 9990794

Found this project in borlands developer book. For anyone intersted!


//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "GetExcel.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm_GetExcel *Form_GetExcel;
//---------------------------------------------------------------------------
__fastcall TForm_GetExcel::TForm_GetExcel(TComponent* Owner)
      : TForm(Owner)
{
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
/*
||
||      Launch Excel
||
*/
void __fastcall TForm_GetExcel::Button_LaunchExcelClick(TObject *Sender)
{
      try
      {
            my_excel = Variant::GetActiveObject("excel.application");      
      }
      catch (...)
      {
            try
            {
                  my_excel = Variant::CreateObject("excel.application");
            }
            catch (...)
            {
                  ShowMessage("GetExcel failed.");
                  return;
            }
      }
      my_excel.OlePropertySet("Visible",(Variant)true);


        //opens worksheet
        Variant      all_workbooks = my_excel.OlePropertyGet("workbooks");
        Procedure Open("Open");
        all_workbooks.Exec(Open << "C:\\my_file2.xls");


}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
/*
||
||
||
*/
void __fastcall TForm_GetExcel::Button_WorkbookClick(TObject *Sender)
{
      Variant      my_workbook;
      Variant      my_worksheet;
      Variant      my_range;

    PropertyGet     Range("Range");
    PropertySet     SetValue("Value");
    PropertySet     SetFormula("Formula");
    PropertyGet     GetValue("Value");
    PropertyGet     GetFormula("Formula");
    PropertyGet     Macro2("Macro2") ;
                       
      my_workbook            = my_excel.OlePropertyGet("ActiveWorkbook");
      my_worksheet      = my_workbook.OlePropertyGet("ActiveSheet");


      my_worksheet.Exec (Macro2);    


    Range.ClearArgs();
    SetValue.ClearArgs();      
    my_range    = my_worksheet.Exec(Range << "A1");
    my_range.Exec(SetValue << "My Excel Worksheet");

    Range.ClearArgs();
    SetValue.ClearArgs();
    my_range    = my_worksheet.Exec(Range << "C2");
    my_range.Exec(SetValue << "1");

   

     //set a value in a cell
    Range.ClearArgs();
    SetValue.ClearArgs();
    my_range    = my_worksheet.Exec(Range << "C3");
    my_range.Exec(SetValue << "2");


    Range.ClearArgs();
    my_range    = my_worksheet.Exec(Range << "B1:B5");
    my_range.Exec(SetFormula << "=rand()");


    //set formula in cell
    Range.ClearArgs();
    SetFormula.ClearArgs();
    my_range    = my_worksheet.Exec(Range << "A2");
    my_range.Exec(SetFormula << "=sum(b1:b5)");

          ShowMessage(my_range.OlePropertyGet("Value"));
      ShowMessage(my_range.OlePropertyGet("Formula"));



      //set a formula in a cell
    Range.ClearArgs();
    SetFormula.ClearArgs();
    my_range    = my_worksheet.Exec(Range << "C4");
    my_range.Exec(SetFormula << "=sum(C2+C3)");

      ShowMessage(my_range.OlePropertyGet("Value"));
      ShowMessage(my_range.OlePropertyGet("Formula"));




          //gets a value and formula from a cell
          Range.ClearArgs();
          SetFormula.ClearArgs();
          my_range    = my_worksheet.Exec(Range << "B9");
          double a =  my_range.OlePropertyGet("Value");
          String For = my_range.OlePropertyGet("Formula");

        // my_worksheet.exec (SolverOk SetCell:="$H$9", MaxMinVal:=2, ValueOf:="0", ByChange:="$B$4:$B$6");

       
               ShowMessage(my_range.OlePropertyGet("Value"));
      ShowMessage(my_range.OlePropertyGet("Formula"));



}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
/*
||
||      Add New Workbook
||
*/
void __fastcall TForm_GetExcel::Button_NewWorkbookClick(TObject *Sender)
{
      Variant      all_workbooks;
      Variant      my_workbook;

      //-- Get workbooks collection
      all_workbooks      = my_excel.OlePropertyGet("Workbooks");

      //-- Set number of worksheets to 1
      my_excel.OlePropertySet("SheetsInNewWorkbook",(Variant)1);

      //-- Create a new workbook
      my_workbook            = all_workbooks.OleFunction("Add");
}
//---------------------------------------------------------------------------




void __fastcall TForm_GetExcel::Button_SaveClick(TObject *Sender)
{
      Variant      my_workbook = my_excel.OlePropertyGet("ActiveWorkbook");

      my_workbook.OleProcedure("Save");
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
/*
||
||
||
*/
void __fastcall TForm_GetExcel::Button_SaveAsClick(TObject *Sender)
{
    Procedure SaveAs("SaveAs");
      Variant      my_workbook = my_excel.OlePropertyGet("ActiveWorkbook");
   
    my_workbook.Exec(SaveAs << "c:\\my_file.xls");
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
/*
||
||
||
*/
void __fastcall TForm_GetExcel::Button_OpenClick(TObject *Sender)
{
      Variant      all_workbooks = my_excel.OlePropertyGet("workbooks");
        Procedure Open("Open");

      //      if (OpenDialog1->Execute())
      {

          all_workbooks.Exec(Open << "C:\\my_file.xls");
        //all_workbooks.Exec(Open << OpenDialog1->FileName);
      }
}
//---------------------------------------------------------------------------







0
 
LVL 9

Expert Comment

by:tinchos
ID: 10356391
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 10356574
Well, two viable solutions have been posted, so I think this one should not be refunded.
0
 

Author Comment

by:JFoster4743
ID: 10391926
I answered this one myself. How do I post that?
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 10392719
JFoster4743 is correct.
And yes, my mistake, this one should be deleted and refunded.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 10392731
Correction AGAIN.  
PAQ not deleted.
0
 

Accepted Solution

by:
amp072397 earned 0 total points
ID: 10394301
Thanks to both of you!!
PAQ/Points refunded (500)

amp
Community Support Moderator
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question