Delphi & Excel

fhbexpir used Ask the Experts™
In Delphi 6.0 with Excel 8.0(2000)
How I can read(or write) a value (or field value) from(in)
workBook in cells of excell.
(How I can creat a relation with excel and delphi)

best regards
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Use TExcelApplication from "Servers" tab.

A small demo programme that show almost everything you can do:

Sources and captions are pretty self explanatory but in French...

If your French is enough to understand "ouvrir", "lire", and "fermer" you should pretty much understand all of it.

Hope it helps:

Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Look in the Excel VBA documentation.  Once an excel notebook is instantiated as a Variant, you can do anything.  I recently posted a large chunk of code that constructs a spreadsheet with formulae, named areas, constant values, and links to other worksheets from a database query.


thanks for your usefull guid

But, I want to show  Excel Application in My form

and it appear into my form no other window

best regards

OK, then this is about the way to go:
Get a new form.
Put an OleContainer in it, call it OleContainer1.
Put also a TExcelApplication, call it xlsAp.
Put a TExcelWorkbook. Call it xlsWB.
Put a TExcelWorksheet. Call it xlsWS.

Put an edit and button to open a file.

The click event handler should be something like:

procedure TForm1.Button6Click(Sender: TObject);
var lcid: integer;
 K, R, X, Y : Integer;
 RangeMatrix : Variant;
  { No need to start Excel ourselves, since the ExcelApplication's AutoConnect
    property is True }
  lcid := GetUserDefaultLCID;

{ To open a new file:
  xlsWB.ConnectTo(xlsApp.Workbooks.Add(TOleEnum(xlWBATWorksheet), lcid)); }
  xlsWB.ConnectTo(xlsApp.Workbooks[1] as _Workbook);
  xlsWS.ConnectTo(xlsWB.Worksheets[1] as _Worksheet);


{ Some stuff you can do: }

  // Get the value of the last row
  X := xlsApp.ActiveCell.Row;
  // Get the value of the last column
  Y := xlsApp.ActiveCell.Column;

{ Lets say you also had a TStringGrid on the form:

  StringGrid1.Cells[0,0]:= 'X: '+inttostr(X);
  StringGrid1.Cells[1,0]:= 'Y: '+inttostr(Y);

  StringGrid1.RowCount:= Y;
  StringGrid1.ColCount:= Y;
  R := StringGrid1.RowCount;

  RangeMatrix := xlsApp.Range['A1',xlsApp.Cells.Item[X,Y]].Value;
//  Define the loop for filling in the TStringGrid
  K := 1;
    for R := 1 to Y do
      StringGrid1.Cells[(R - 1),(K)] := RangeMatrix[K,R];
    StringGrid1.RowCount := K + 1;
  until K > X;


//  xlsWS.Range['A1','A1'].Value := 'Hello World'; //That's how to write on it.

{ Even more fun stuff: }

  vMSExcel.ActiveWorkbook.Worksheets[1].Range['A1'].Value := 'Hello World';
  vMSExcel.ActiveCell.FormulaR1C1 := '1/20/2003';
  vMSExcel.Selection.Interior.ColorIndex := 6;



Just a warning: this is *sort of* the way to do it. But this code is probably flawed and will either not compile or raise exceptions. You will need to read some helps to get it to work, but I hope it wil put you on the right path. =)

Hope it helps.





very very thanks for your guids.
sorry,don't soloution my problem.
I want to show excel environment in my form
please tel me,what help should be read.

please send your E-Mail Address to send pic of my request
thanks best regards

You want to have all the buttons and menu options of Excel in your app?
Something similar to what happens when you open an xls with Internet Explorer? (Or a pdf and you get an instance of Acrobat Reader running within IE).

I've never done that.

My guess is you do it with OLE Automation just like the last example but that you have to fiddle a little with the properties of the TOLEContainer.

The helps for TOLEContainer (for Delphi 5) mention the possibility of doing it. Here goes an excerpt from the description for the TOLEContainer. Reading about it's properties, methods and events should lead you where you're going in no time at all. (I hope...)


Use TOleContainer to handle many of the complexities of OLE 2.0. TOleContainer lets the user choose an OLE object to insert by simply calling the InsertObjectDialog method. TOleContainer can create either an embedded OLE object or a linked OLE object.

TOleContainer automatically handles menu merging -- the process of combining the container form's menu with that of an in-place activated OLE object's server application. The menu items' GroupIndex property settings control how menus are merged. Those main menu items with GroupIndex values of 0, 2, and 4 remain; TOleContainer merges the server application's menus and replaces the main menu items with GroupIndex values of 1, 3, and 5 (if they exist).

OLE objects that are activated in-place add their servers' toolbars directly into the container application's window. Normally, any panels used for toolbars are replaced by the OLE object's server's toolbars. Prevent this by setting a panel's Locked property to True.

When using TOleContainer in an SDI application (the main form's FormStyle property is fsNormal rather than fsMDIForm), place the TOleContainer component inside a panel whose Align property is set to alClient. TOleContainer replaces an SDI form's toolbars as described above, and using a panel automatically adjusts the amount of space available to the OLE container.

If you still feel I haven't understood you correctly, you can mail me that image at <>




I've played quite a bit with OLEContainers... :-)
If I understand where your problem is, the point
relies on the fact you don't see the File menu, right?

Unfortunately, File menu is always hidden, thus you
have to show it through automation.



hi, You can use ADO with delphi

Put an ADOQuery on your form

MyAdoQuery.Connectionstring := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MyExcelFile.xls;Extended Properties=Excel 8.0';


MyADOQuery := 'SELECT * FROM [$sheet1]';;

handle data as normal

uses word2000
  function TExlApp.OpenWorkbook(AWorkbookPath: string = '';
                              ANewWorkbook: boolean = true): IXLWorkbook;
    FIWorkbook := Workbooks.Add(TemplateDirPath + AWorkbookPath, FLCID);
  if (Trim(AWorkbookPath) <> '') and not(ANewWorkbook) then
    FIWorkbook := Workbooks.Open(AWorkbookPath, EmptyParam, EmptyParam,
      EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
      EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, FLCID);
procedure wirteExcel(WorkSheetName:string;yourPar:string;RowNum,ColNum:integer)
var ISheet: _Worksheet
  ISheet:=FIWorkbook.Worksheets.Item[AWorksheetName]as _Worksheet;

{you can use VBA like this,Hope it helps.}

I think ADO is the best solution in this case. Unfortunatly (as I know) you can't format the cells with ADO ( I mean: background color, etc...).
With ADO you can handle excel files like TTables. You can put some ADODataSet's to a form, and the cells is like in excel.


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial