Solved

Accessing Excel in C++ Builder 5.0

Posted on 2003-12-11
11
2,194 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Load and store *.pnm image file 1 75
Move constructor only called if marked noexcept? 6 98
Find Visual Studio Tools 2 92
best sources to up-to-date in C++? 8 70
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

932 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

9 Experts available now in Live!

Get 1:1 Help Now