Solved

Delphi & Excel

Posted on 2003-10-28
12
52,028 Views
Last Modified: 2011-08-18
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)

Thanks
best regards
0
Comment
Question by:fhbexpir
  • 3
  • 2
  • 2
  • +5
12 Comments
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 50 total points
Comment Utility
Use TExcelApplication from "Servers" tab.
0
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
0
 
LVL 1

Expert Comment

by:JackNaif
Comment Utility
A small demo programme that show almost everything you can do:

http://www.phidels.com/php/zip/oleexcel.zip

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:

Jack
0
 
LVL 6

Expert Comment

by:swift99
Comment Utility
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.
0
 

Author Comment

by:fhbexpir
Comment Utility
thanks for your usefull guid

But, I want to show  Excel Application in My form

and it appear into my form no other window

Thanks
best regards
0
 
LVL 1

Expert Comment

by:JackNaif
Comment Utility
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;
begin
olecontainer1.CreateLinkToFile(edit1.text,false);
  { No need to start Excel ourselves, since the ExcelApplication's AutoConnect
    property is True }
  lcid := GetUserDefaultLCID;
  xlsApp.Visible[lcid]:=true;

  xlsApp.Workbooks.Open(edit1.text,null,null,null,null,null,null,null,null,null,null,null,null,lcid);
{ 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);
  xlsWS.Activate;

  xlsWS.Cells.SpecialCells(xlCellTypeLastCell,EmptyParam).Activate;

{ 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;
  repeat
    for R := 1 to Y do
      StringGrid1.Cells[(R - 1),(K)] := RangeMatrix[K,R];
    Inc(K,1);
    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.range['a2']:='11:29:42';
  vMSExcel.Selection.EntireRow.Insert;
  vMSExcel.ActiveCell.FormulaR1C1 := '1/20/2003';
  vMSExcel.Range['A2','b3'].Select;
  vMSExcel.Selection.Interior.ColorIndex := 6;

 }

end;


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.

Regards:

Jack
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:fhbexpir
Comment Utility
Hi

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
0
 
LVL 1

Expert Comment

by:JackNaif
Comment Utility
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...)

-------------------------------------------
Description

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 <wgrillo@fi.uba.ar>

Regards:

Jack
0
 
LVL 5

Expert Comment

by:DeerBear
Comment Utility
Hi,

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.

HTH,

Andrew
0
 
LVL 2

Expert Comment

by:joepezt
Comment Utility
hi, You can use ADO with delphi

Put an ADOQuery on your form
then

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


then

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

handle data as normal
0
 

Expert Comment

by:BlueTom
Comment Utility
uses word2000
const
FLCID = LOCALE_USER_DEFAULT;
....
  function TExlApp.OpenWorkbook(AWorkbookPath: string = '';
                              ANewWorkbook: boolean = true): IXLWorkbook;
var
    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);
end;
procedure wirteExcel(WorkSheetName:string;yourPar:string;RowNum,ColNum:integer)
var ISheet: _Worksheet
begin
  ISheet:=FIWorkbook.Worksheets.Item[AWorksheetName]as _Worksheet;
 ISheet.Cells.Item[RowNum,ColNum].FormulaR1C1:=yourPar;
end;

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

Expert Comment

by:d43m0n
Comment Utility
Hi
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.

Bye.


0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

743 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

16 Experts available now in Live!

Get 1:1 Help Now