View Full Version : Urgent: I need an ExcelApplication sample for D6
ahmetax
09-27-2001, 11:46 AM
Hi,
I need a simple ExcelApplication sample in Delphi6.
I searched for it on the web. Tried the samples from Deborah Pate's pages. But, I couln't succeed. It looks like there are some important changes between Delphi 5 and Delphi 6.
Can anybody send me a very simple example which will run under Delphi 6 using a TExcelApplication component.
1- Open TExcelApplication
2- Open a workbook
3- Open a worksheet
4- write something on the sheet
Thanks.
ax
MrBaseball34
10-01-2001, 07:46 AM
Here is a sample tht I found on experts-exchange that
wil open a workbook and load the values into a stringlist.
It shows how to get to the data...Works in D6...Maybe it
will help...
procedure TForm1.Button1Click(Sender: TObject);
var
strWBName : String;
strWsName : String;
WorkBk : _WorkBook;
WorkSheet : _WorkSheet;
K, R, X, Y : Integer;
intX : Integer;
RangeMatrix : Variant;
blnSheetExist : Boolean;
FxCols, FxRows: Integer;
begin
{ Initialise some variables. }
strWBName := 'c:\Book4.xls';
strWsName := 'Sheet1';
{ Start Excel-Connection }
XLApp.Connect;
// Open the Excel File
XLApp.WorkBooks.Open(strWBName,EmptyParam,EmptyPar am,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyP aram,
EmptyParam,EmptyParam,EmptyParam,0);
WorkBk := XLApp.WorkBooks.Item[1];
blnSheetExist := False;
intX:=1;
While intX <= XLApp.Worksheets.Count do
begin
WorkSheet := WorkBk.Worksheets.Get_Item(intX) as _WorkSheet;
if WorkSheet.Name <> strWsName then
Inc(IntX)
else
begin
blnSheetExist := True;
Break;
end;
end;
if not blnSheetExist then
begin
MessageDlg('No Sheet exist with name : ' + strWsName, mtError, [mbOK], 0);
end
else
begin
WorkSheet := WorkBk.Worksheets.Get_Item(intX) as _WorkSheet;
// In order to know the dimension of the WorkSheet, i.e the number of rows and the
// number of columns, we activate the last non-empty cell of it
WorkSheet.Activate(intX);
WorkSheet.Cells.SpecialCells(xlCellTypeLastCell,Em ptyParam).Activate;
// Get the value of the last row + column
X := XLApp.ActiveCell.Row;
Y := XLApp.ActiveCell.Column;
// Define the number of the columns in the TStringGrid
StringGrid1.ColCount := Y;
// Assign the Variant associated with the WorkSheet to the Delphi Variant Matrix
RangeMatrix := XLApp.Range['A1',XLApp.Cells.Item[X,Y]].Value;
// Quit Excel and Disconnect the Server
XLApp.Workbooks.Close(1);
XLApp.Quit;
XLApp.Disconnect;
// Define the loop for filling in the TStringGrid
K := 1;
FxCols := 0;
FxRows := 0;
if StringGrid1.FixedCols > 0 then
FxCols := 1 - StringGrid1.FixedCols;
if StringGrid1.FixedRows > 0 then
FxRows := 1 - StringGrid1.FixedRows;
repeat
for R := 1 to Y do
StringGrid1.Cells[(R - FxCols),(K - FxRows)] := RangeMatrix[K,R];
Inc(K,1);
StringGrid1.RowCount := K + 1;
until K > X;
// Unassign the Delphi Variant Matrix
RangeMatrix := Unassigned;
end;
end;
ahmetax
10-01-2001, 09:49 AM
Thank you very much MrBaseball34.
Your sample code will be a very good starting point for me. In order to run it, I just removed 2 lines of code. I don't know the reason yet, but it worked for me.
...
//WorkSheet.Activate(intX);
...
//XLApp.Workbooks.Close(1);
...
Thanks a lot.
ax
ahmetax
10-01-2001, 09:54 AM
I must add the error message I got from both lines. When I remarked those lines, the code ran perfectly:
"Project Project1.exe reaised class EOleExcepiton with message 'Old format or invalis type library'. Process stopped. Use Step or Run to continue.
ax
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.