Delphi & Excel

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use TExcelApplication from "Servers" tab.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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:

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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.
fhbexpirAuthor Commented:
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.


fhbexpirAuthor Commented:

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.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.