Solved

Accessing Excel in C++ Builder 5.0

Posted on 2003-12-11
11
2,205 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
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 149
Would like to move button in a function 3 76
Exception thrown at 0x00007FFD5BC81F28 7 43
C++ mouse_event mouse look 7 82
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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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