Solved

Accessing Excel in C++ Builder 5.0

Posted on 2003-12-11
11
2,188 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
 

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now