 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
JZ Guest
|
Posted: Mon Aug 08, 2005 8:29 pm Post subject: Need to read data from Excel -- where to begin |
|
|
I need a recommendation about simple examples of reading an Excel file into
an application. I am using the components in Delphi 7.
thanks
|
|
| Back to top |
|
 |
Crazy Horse's crazier lit Guest
|
Posted: Mon Aug 08, 2005 9:06 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
"JZ" <ddd> wrote
| Quote: | I need a recommendation about simple examples of reading an Excel file into
an application. I am using the components in Delphi 7.
|
One way is to save the Excel file as CSV, and then load that file into a
string list and iterate through it.
--
Download Blackbird Crow Raven's book
"STILL CASTING SHADOWS: Two American Families 1620-2006"
(.exe and .pdf): http://cc.borland.com/ccweb.exe/listing?id=23106
|
|
| Back to top |
|
 |
Patrick Guest
|
Posted: Mon Aug 08, 2005 9:19 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
|
|
| Back to top |
|
 |
JZ Guest
|
Posted: Tue Aug 09, 2005 12:51 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
Much of that information is related the Delphi 5 and does not appear to be
correct for Delphi 7. Is there nothing better?
"Patrick" <pdc (AT) novusprintmedia (DOT) com> wrote
|
|
| Back to top |
|
 |
Rick Francken Guest
|
Posted: Tue Aug 09, 2005 1:10 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
Which information do you think is not correct?
I've done several Excel automation projects, and everything that works in
Delphi 5 also works in Delphi 7. The bigger concern might be the
differences between versions of the Excel type library.
"JZ" <ddd> wrote
| Quote: | Much of that information is related the Delphi 5 and does not appear to be
correct for Delphi 7. Is there nothing better?
"Patrick" <pdc (AT) novusprintmedia (DOT) com> wrote in message
news:42f7cc21$1 (AT) newsgroups (DOT) borland.com...
I need a recommendation about simple examples of reading an Excel file
into an application. I am using the components in Delphi 7.
http://www.djpate.freeserve.co.uk/Automation.htm
|
|
|
| Back to top |
|
 |
JZ Guest
|
Posted: Tue Aug 09, 2005 1:25 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
For example, I tried the line:
ExcelWorksheet1.ConnectTo(ExcelApplication1.ActiveSheet as _Worksheet);And I
found that ExcelApplication1 does not have the property "ActiveSheet"
Therefor I am stuck!
Thank,
"Rick Francken" <rick (AT) franckensoft (DOT) com> wrote
| Quote: | Which information do you think is not correct?
I've done several Excel automation projects, and everything that works in
Delphi 5 also works in Delphi 7. The bigger concern might be the
differences between versions of the Excel type library.
"JZ" <ddd> wrote
Much of that information is related the Delphi 5 and does not appear to
be correct for Delphi 7. Is there nothing better?
"Patrick" <pdc (AT) novusprintmedia (DOT) com> wrote in message
news:42f7cc21$1 (AT) newsgroups (DOT) borland.com...
I need a recommendation about simple examples of reading an Excel file
into an application. I am using the components in Delphi 7.
http://www.djpate.freeserve.co.uk/Automation.htm
|
|
|
| Back to top |
|
 |
JZ Guest
|
Posted: Tue Aug 09, 2005 1:27 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
Sorry my formating had issues in previous message. This should be clearer.
ExcelWorksheet1.ConnectTo(ExcelApplication1.ActiveSheet as _Worksheet);
And I found that ExcelApplication1 does not have the property "ActiveSheet"
Therefor I am stuck!
|
|
| Back to top |
|
 |
Patrick Guest
|
Posted: Tue Aug 09, 2005 2:16 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
| Quote: | I've done several Excel automation projects, and everything that works in
Delphi 5 also works in Delphi 7. The bigger concern might be the
differences between versions of the Excel type library.
Rick is right on the money. I use late binding mostly, and from what I've |
seen from Deborah's site, everything should work in D7 (I used it as a
starting point.)
I use Excel automation often, and have found this combo to work well: Use
late binding, Deborah's site + NG serarches for the delphi mechanics. I use
Excel's VBA Object inspector to determine what properties and methods I have
access to.
Here is an example of creating a recordset and reading data into it, from an
excel file. There is extra stuff in there (progressbars and such) feel free
to ignore. In this case it stops reading columns when it hits a blank, and
rows when it hits a blank. Our recordsets cannot have spaces in the
fieldnames so I replace spaces with '_'
Screen.Cursor := crSpinningN;
try
try
oExcel := GetActiveOleObject('Excel.Application') ;
except
oExcel := CreateOleObject('Excel.Application') ;
end;
oWBooks := oExcel.Workbooks;
oWBooks.Open(strFile, 0, True, 1, '', '', True, $00000002, '', True,
False, 0, True);
oExcel.Visible := False;
rsRawData := CoRecordset.Create;
FldAttribs := adFldUpdatable+adFldIsNullable+adFldMayBeNull;
i := 1;
while
(NOT VarIsNull(oExcel.Cells.Item[1,i])
AND (VarToStr(oExcel.Cells.Item[1,i]) <> ''))
do
begin
// Create The RS
RSFieldsAppend(rsRawData,
StringReplace(Trim(VarToStr(oExcel.Cells.Item[1,i])), #32, '_',
[rfReplaceAll]), adVarChar, 200, FldAttribs);
i := i + 1;
iFieldCount := i;
end;
if rsRawData.Fields.Count > 0 then
begin
if NOT RSContainsField(rsRawData, 'LookupName') then
RSFieldsAppend(rsRawData, 'LookupName', adVarChar, 50,
FldAttribs);
if NOT RSContainsField(rsRawData, 'LookupID') then
RSFieldsAppend(rsRawData, 'LookupID', adInteger,
SizeOf(Integer), FldAttribs);
if NOT RSContainsField(rsRawData, 'CF_Ord') then
RSFieldsAppend(rsRawData, 'CF_Ord', adInteger, SizeOf(Integer),
FldAttribs);
end;
if rsRawData <> nil then
Variant(rsRawData).Open;
Progress_FO := TnProgressForm.Create(frmMain);
try
Progress_FO.Caption := 'In Process...';
Progress_FO.lblOperation.Caption := 'Loading Source File';
Progress_FO.gagStatus.MinValue := 2;
if oExcel.Cells.SpecialCells(xlLastCell).Row >= c_MaxRowsToImport
then
Progress_FO.gagStatus.MaxValue := c_MaxRowsToImport
else if oExcel.Cells.SpecialCells(xlLastCell).Row > 3 then
Progress_FO.gagStatus.MaxValue :=
oExcel.Cells.SpecialCells(xlLastCell).Row
else
Progress_FO.gagStatus.MaxValue := 3;
if (Progress_FO.gagStatus.MaxValue >= 100) then
Progress_FO.Show;
i := 1;
j := 2;
while
(j <= c_MaxRowsToImport)
AND
(NOT VarIsNull(oExcel.Cells.Item[j,1])
AND (VarToStr(oExcel.Cells.Item[j,2]) <> ''))
do
begin
Variant(rsRawData).AddNew;
i := 1;
while
((i > 1) AND (i <= iFieldCount))
OR
((i = 1) AND NOT VarIsNull(oExcel.Cells.Item[j,i])
AND (VarToStr(oExcel.Cells.Item[j,i]) <> ''))
do
begin
RSField(rsRawData, Pred(i)).Value :=
oExcel.Cells.Item[j,i].Text;
i := i + 1;
end;
RSField(rsRawData, 'CF_Ord').Value := j - 1;
j := j + 1;
Progress_FO.gagStatus.AddProgress(1);
Progress_FO.Repaint;
end;
if Progress_FO <> nil then
Progress_FO.Close;
finally
Progress_FO.Free;
end;
oWBooks.Close;
if NOT(VarIsEmpty(oExcel)) then
oExcel := Unassigned;
if NOT(VarIsEmpty(oWBooks)) then
oWBooks := Unassigned;
finally
Screen.Cursor := crDefault;
end;
|
|
| Back to top |
|
 |
JZ Guest
|
Posted: Tue Aug 09, 2005 2:35 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
Many thanks, I will start working through this.
"Patrick" <pdc (AT) novusprintmedia (DOT) com> wrote
| Quote: | I've done several Excel automation projects, and everything that works in
Delphi 5 also works in Delphi 7. The bigger concern might be the
differences between versions of the Excel type library.
Rick is right on the money. I use late binding mostly, and from what I've
seen from Deborah's site, everything should work in D7 (I used it as a
starting point.)
I use Excel automation often, and have found this combo to work well: Use
late binding, Deborah's site + NG serarches for the delphi mechanics. I
use Excel's VBA Object inspector to determine what properties and methods
I have access to.
Here is an example of creating a recordset and reading data into it, from
an excel file. There is extra stuff in there (progressbars and such) feel
free to ignore. In this case it stops reading columns when it hits a
blank, and rows when it hits a blank. Our recordsets cannot have spaces in
the fieldnames so I replace spaces with '_'
Screen.Cursor := crSpinningN;
try
try
oExcel := GetActiveOleObject('Excel.Application') ;
except
oExcel := CreateOleObject('Excel.Application') ;
end;
oWBooks := oExcel.Workbooks;
oWBooks.Open(strFile, 0, True, 1, '', '', True, $00000002, '', True,
False, 0, True);
oExcel.Visible := False;
rsRawData := CoRecordset.Create;
FldAttribs := adFldUpdatable+adFldIsNullable+adFldMayBeNull;
i := 1;
while
(NOT VarIsNull(oExcel.Cells.Item[1,i])
AND (VarToStr(oExcel.Cells.Item[1,i]) <> ''))
do
begin
// Create The RS
RSFieldsAppend(rsRawData,
StringReplace(Trim(VarToStr(oExcel.Cells.Item[1,i])), #32, '_',
[rfReplaceAll]), adVarChar, 200, FldAttribs);
i := i + 1;
iFieldCount := i;
end;
if rsRawData.Fields.Count > 0 then
begin
if NOT RSContainsField(rsRawData, 'LookupName') then
RSFieldsAppend(rsRawData, 'LookupName', adVarChar, 50,
FldAttribs);
if NOT RSContainsField(rsRawData, 'LookupID') then
RSFieldsAppend(rsRawData, 'LookupID', adInteger,
SizeOf(Integer), FldAttribs);
if NOT RSContainsField(rsRawData, 'CF_Ord') then
RSFieldsAppend(rsRawData, 'CF_Ord', adInteger, SizeOf(Integer),
FldAttribs);
end;
if rsRawData <> nil then
Variant(rsRawData).Open;
Progress_FO := TnProgressForm.Create(frmMain);
try
Progress_FO.Caption := 'In Process...';
Progress_FO.lblOperation.Caption := 'Loading Source File';
Progress_FO.gagStatus.MinValue := 2;
if oExcel.Cells.SpecialCells(xlLastCell).Row >= c_MaxRowsToImport
then
Progress_FO.gagStatus.MaxValue := c_MaxRowsToImport
else if oExcel.Cells.SpecialCells(xlLastCell).Row > 3 then
Progress_FO.gagStatus.MaxValue :=
oExcel.Cells.SpecialCells(xlLastCell).Row
else
Progress_FO.gagStatus.MaxValue := 3;
if (Progress_FO.gagStatus.MaxValue >= 100) then
Progress_FO.Show;
i := 1;
j := 2;
while
(j <= c_MaxRowsToImport)
AND
(NOT VarIsNull(oExcel.Cells.Item[j,1])
AND (VarToStr(oExcel.Cells.Item[j,2]) <> ''))
do
begin
Variant(rsRawData).AddNew;
i := 1;
while
((i > 1) AND (i <= iFieldCount))
OR
((i = 1) AND NOT VarIsNull(oExcel.Cells.Item[j,i])
AND (VarToStr(oExcel.Cells.Item[j,i]) <> ''))
do
begin
RSField(rsRawData, Pred(i)).Value :=
oExcel.Cells.Item[j,i].Text;
i := i + 1;
end;
RSField(rsRawData, 'CF_Ord').Value := j - 1;
j := j + 1;
Progress_FO.gagStatus.AddProgress(1);
Progress_FO.Repaint;
end;
if Progress_FO <> nil then
Progress_FO.Close;
finally
Progress_FO.Free;
end;
oWBooks.Close;
if NOT(VarIsEmpty(oExcel)) then
oExcel := Unassigned;
if NOT(VarIsEmpty(oWBooks)) then
oWBooks := Unassigned;
finally
Screen.Cursor := crDefault;
end;
|
|
|
| Back to top |
|
 |
JZ Guest
|
Posted: Tue Aug 09, 2005 3:14 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
Is it possible for you to add the declarations to this?
"Patrick" <pdc (AT) novusprintmedia (DOT) com> wrote
| Quote: | I've done several Excel automation projects, and everything that works in
Delphi 5 also works in Delphi 7. The bigger concern might be the
differences between versions of the Excel type library.
Rick is right on the money. I use late binding mostly, and from what I've
seen from Deborah's site, everything should work in D7 (I used it as a
starting point.)
I use Excel automation often, and have found this combo to work well: Use
late binding, Deborah's site + NG serarches for the delphi mechanics. I
use Excel's VBA Object inspector to determine what properties and methods
I have access to.
Here is an example of creating a recordset and reading data into it, from
an excel file. There is extra stuff in there (progressbars and such) feel
free to ignore. In this case it stops reading columns when it hits a
blank, and rows when it hits a blank. Our recordsets cannot have spaces in
the fieldnames so I replace spaces with '_'
Screen.Cursor := crSpinningN;
try
try
oExcel := GetActiveOleObject('Excel.Application') ;
except
oExcel := CreateOleObject('Excel.Application') ;
end;
oWBooks := oExcel.Workbooks;
oWBooks.Open(strFile, 0, True, 1, '', '', True, $00000002, '', True,
False, 0, True);
oExcel.Visible := False;
rsRawData := CoRecordset.Create;
FldAttribs := adFldUpdatable+adFldIsNullable+adFldMayBeNull;
i := 1;
while
(NOT VarIsNull(oExcel.Cells.Item[1,i])
AND (VarToStr(oExcel.Cells.Item[1,i]) <> ''))
do
begin
// Create The RS
RSFieldsAppend(rsRawData,
StringReplace(Trim(VarToStr(oExcel.Cells.Item[1,i])), #32, '_',
[rfReplaceAll]), adVarChar, 200, FldAttribs);
i := i + 1;
iFieldCount := i;
end;
if rsRawData.Fields.Count > 0 then
begin
if NOT RSContainsField(rsRawData, 'LookupName') then
RSFieldsAppend(rsRawData, 'LookupName', adVarChar, 50,
FldAttribs);
if NOT RSContainsField(rsRawData, 'LookupID') then
RSFieldsAppend(rsRawData, 'LookupID', adInteger,
SizeOf(Integer), FldAttribs);
if NOT RSContainsField(rsRawData, 'CF_Ord') then
RSFieldsAppend(rsRawData, 'CF_Ord', adInteger, SizeOf(Integer),
FldAttribs);
end;
if rsRawData <> nil then
Variant(rsRawData).Open;
Progress_FO := TnProgressForm.Create(frmMain);
try
Progress_FO.Caption := 'In Process...';
Progress_FO.lblOperation.Caption := 'Loading Source File';
Progress_FO.gagStatus.MinValue := 2;
if oExcel.Cells.SpecialCells(xlLastCell).Row >= c_MaxRowsToImport
then
Progress_FO.gagStatus.MaxValue := c_MaxRowsToImport
else if oExcel.Cells.SpecialCells(xlLastCell).Row > 3 then
Progress_FO.gagStatus.MaxValue :=
oExcel.Cells.SpecialCells(xlLastCell).Row
else
Progress_FO.gagStatus.MaxValue := 3;
if (Progress_FO.gagStatus.MaxValue >= 100) then
Progress_FO.Show;
i := 1;
j := 2;
while
(j <= c_MaxRowsToImport)
AND
(NOT VarIsNull(oExcel.Cells.Item[j,1])
AND (VarToStr(oExcel.Cells.Item[j,2]) <> ''))
do
begin
Variant(rsRawData).AddNew;
i := 1;
while
((i > 1) AND (i <= iFieldCount))
OR
((i = 1) AND NOT VarIsNull(oExcel.Cells.Item[j,i])
AND (VarToStr(oExcel.Cells.Item[j,i]) <> ''))
do
begin
RSField(rsRawData, Pred(i)).Value :=
oExcel.Cells.Item[j,i].Text;
i := i + 1;
end;
RSField(rsRawData, 'CF_Ord').Value := j - 1;
j := j + 1;
Progress_FO.gagStatus.AddProgress(1);
Progress_FO.Repaint;
end;
if Progress_FO <> nil then
Progress_FO.Close;
finally
Progress_FO.Free;
end;
oWBooks.Close;
if NOT(VarIsEmpty(oExcel)) then
oExcel := Unassigned;
if NOT(VarIsEmpty(oWBooks)) then
oWBooks := Unassigned;
finally
Screen.Cursor := crDefault;
end;
|
|
|
| Back to top |
|
 |
Patrick Guest
|
Posted: Tue Aug 09, 2005 3:19 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
"JZ" <ddd> wrote
| Quote: | Is it possible for you to add the declarations to this?
Whoops, sorry: |
oExcel : OleVariant;
oWBooks : OleVariant;
i, j : Integer;
FldAttribs: FieldAttributeEnum;
Progress_FO: TnProgressForm;
iFieldCount : Integer;
|
|
| Back to top |
|
 |
Crazy Horse's crazier lit Guest
|
Posted: Tue Aug 09, 2005 3:22 pm Post subject: Re: Need to read data from Excel -- where to begin |
|
|
"JZ" <ddd> wrote
| Quote: | Is it possible for you to add the declarations to this?
|
You can name the method anything you like.
For example, if the code is going in a form unit, you can do this:
1. In the private section, add procedure Whatever()
2. R-click and select "Complete Class at Cursor"
3. Paste Patrick's code between the begin and end
4. Add the necessary variables
5. Test and tweak
BTW, it's not usually necessary to quote humongous code snippets in your
replies.
--
Download Blackbird Crow Raven's book
"STILL CASTING SHADOWS: Two American Families 1620-2006"
(.exe and .pdf): http://cc.borland.com/ccweb.exe/listing?id=23106
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|