?
Solved

Accessing Excel in C++ Builder 5.0

Posted on 2003-12-11
11
Medium Priority
?
2,260 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
Independent Software Vendors: 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

800 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