views:

115

answers:

2

I have an Excel 2003 sheets being used by clients.

I need to update a Delphi application that extracts data from these cells, but the cells are not named at all, they are just j3 and j55.

Can the data from cells still be sent across to other applications?

Can Delphi application take these values?

A: 

Sure, why not?

You could use Excel Object Model & use the series of classes to access the contents of the cell.

VB(script) example

dim xlApp
set xlApp = CreateObject("Excel.Application")

dim wkBook
set wkBook = xlApp.WorkBooks.Add

dim wkSheet
set wkSheet = wkBook.Sheets(1)

dim cell
set cell = wkSheet.Cells("A1")  'get the reference to cell A1

msgbox cell.Value

The best way to learn about Excel object model is to
1. Open Excel
2. Press ALT + F11 (VBA editor)
3. Press F2 (for object browser)
4. Choose Excel from the dropdown where it shows

The root class is Application & then things flow from there.
e.g. WorkBook -> WorkSheets -> Worksheet -> Cells (Range) etc.

Hope that helps.

shahkalpesh
+5  A: 

You can process Excel files through Delphi using OLE.

uses
  ComObj;

procedure TForm3.btn1Click(Sender: TObject);
var
  ExcelApp: OleVariant;
begin    
  try    
      ExcelApp := CreateOleObject('Excel.Application');
      if not VarIsEmpty(ExcelApp) then
      Begin
       ExcelApp.Workbooks.Open('c:\yourfile.xls'); //Open File
       ShowMessage(ExcelApp.Range['J55', 'J55'].Value);   //Extract value from Cell J55
      End;    

  finally

    if not VarIsEmpty(ExcelApp) then
    begin
      ExcelApp.DisplayAlerts := False;
      ExcelApp.Quit;
    end;

  end;

end;

Bye.

RRUZ
A good way to "work out" what is needed is to record a macro in Excel, then translate the macro code from VBA to Delphi
Gerry