Delphi and Kylix

Moderators: pritaeas
Number of threads: 7264
Number of posts: 19073

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
data from Delphi to Excel Posted by _yilmaz on 18 Jan 2005 at 7:56 AM
Hi;

I have a string grid filled with data. Is there a simple way to transfer this data directly into an MS Excel file?

Thanx...
Report
Re: data from Delphi to Excel Posted by zibadian on 18 Jan 2005 at 9:09 AM
: Hi;
:
: I have a string grid filled with data. Is there a simple way to transfer this data directly into an MS Excel file?
:
: Thanx...
:
Yes, there is. You can access excel files using the excel interface. From there you can access the individual worksheets and cells. This Interface is very poorly documented by ms, so finding the type-casting is quite tricky. I'm currently working on a shell object to access it, but it has quite a slow progress. When I'm done, I will post it in the codepedia, and send you a ph-mail about it.
Report
Re: data from Delphi to Excel Posted by rajsha on 18 Jan 2005 at 10:36 AM
: : Hi;
: :
: : I have a string grid filled with data. Is there a simple way to transfer this data directly into an MS Excel file?
: :
: : Thanx...
: :
: Yes, there is. You can access excel files using the excel interface. From there you can access the individual worksheets and cells. This Interface is very poorly documented by ms, so finding the type-casting is quite tricky. I'm currently working on a shell object to access it, but it has quite a slow progress. When I'm done, I will post it in the codepedia, and send you a ph-mail about it.
:
Here's a procedure to send a dataset's data to Excel. With some intelligeent programming I'm sure u can do away with the dataset specific code and replace it with string grid relevant code. I got it from somewhere on the net and modified it for my purpose!

procedure SendToExcel(aDataSet: TDataSet);
var
PreviewToExcel: TExcelApplication;
RangeE: ExcelRange; //or RangeE: Excel97.Range
I, Row: Integer;
Bookmark: TBookmarkStr;
begin
PreviewToExcel := TExcelApplication.Create(Self);
PreviewToExcel.Connect;
PreviewToExcel.Workbooks.Add(NULL, 0);
RangeE := PreviewToExcel.ActiveCell;

for I := 0 to aDataSet.Fields.Count - 1 do
begin
RangeE.Value := aDataSet.Fields[I].DisplayLabel;
RangeE := RangeE.Next;
end;

aDataSet.DisableControls;
try
Bookmark := aDataSet.Bookmark;
try
aDataSet.First;
Row := 2;
while not aDataSet.EOF do
begin
//Write down Record As Row in msExcel
RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)];
for I := 0 to aDataSet.Fields.Count - 1 do
begin
RangeE.Value := aDataSet.Fields[I].AsString;
RangeE := RangeE.Next;
end;
aDataSet.Next;
Inc(Row);
end;
finally
aDataSet.Bookmark := Bookmark;
end;
finally
aDataSet.EnableControls;
end;

RangeE := PreviewToExcel.Range['A1', chr(64 + aDataSet.Fields.Count) + IntToStr(Row - 1)];

RangeE.AutoFormat(8, NULL, NULL, NULL, NULL, NULL, NULL);
PreviewToExcel.Visible[0] := True;
PreviewToExcel.Disconnect;
Report
Re: data from Delphi to Excel Posted by _yilmaz on 19 Jan 2005 at 6:06 AM
thank you both, your helps are really appreciated.

: : : Hi;
: : :
: : : I have a string grid filled with data. Is there a simple way to transfer this data directly into an MS Excel file?
: : :
: : : Thanx...
: : :
: : Yes, there is. You can access excel files using the excel interface. From there you can access the individual worksheets and cells. This Interface is very poorly documented by ms, so finding the type-casting is quite tricky. I'm currently working on a shell object to access it, but it has quite a slow progress. When I'm done, I will post it in the codepedia, and send you a ph-mail about it.
: :
: Here's a procedure to send a dataset's data to Excel. With some intelligeent programming I'm sure u can do away with the dataset specific code and replace it with string grid relevant code. I got it from somewhere on the net and modified it for my purpose!
:
: procedure SendToExcel(aDataSet: TDataSet);
: var
: PreviewToExcel: TExcelApplication;
: RangeE: ExcelRange; //or RangeE: Excel97.Range
: I, Row: Integer;
: Bookmark: TBookmarkStr;
: begin
: PreviewToExcel := TExcelApplication.Create(Self);
: PreviewToExcel.Connect;
: PreviewToExcel.Workbooks.Add(NULL, 0);
: RangeE := PreviewToExcel.ActiveCell;
:
: for I := 0 to aDataSet.Fields.Count - 1 do
: begin
: RangeE.Value := aDataSet.Fields[I].DisplayLabel;
: RangeE := RangeE.Next;
: end;
:
: aDataSet.DisableControls;
: try
: Bookmark := aDataSet.Bookmark;
: try
: aDataSet.First;
: Row := 2;
: while not aDataSet.EOF do
: begin
: //Write down Record As Row in msExcel
: RangeE := PreviewToExcel.Range['A' + IntToStr(Row), 'A' + IntToStr(Row)];
: for I := 0 to aDataSet.Fields.Count - 1 do
: begin
: RangeE.Value := aDataSet.Fields[I].AsString;
: RangeE := RangeE.Next;
: end;
: aDataSet.Next;
: Inc(Row);
: end;
: finally
: aDataSet.Bookmark := Bookmark;
: end;
: finally
: aDataSet.EnableControls;
: end;
:
: RangeE := PreviewToExcel.Range['A1', chr(64 + aDataSet.Fields.Count) + IntToStr(Row - 1)];
:
: RangeE.AutoFormat(8, NULL, NULL, NULL, NULL, NULL, NULL);
: PreviewToExcel.Visible[0] := True;
: PreviewToExcel.Disconnect;
:

Report
Re: data from Delphi to Excel Posted by _yilmaz on 26 Jan 2005 at 10:13 AM
Hi;

1) When we write a long string in a cell in Excel, it spans just a single line on that cell. Is it possible to make Excel change it automatically so that the string is easily viewed by a user? (inside Delphi code)

2) How can i change the weights of the borders of a cell inside Delphi code?

Thanx...



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.