Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Exporting Grid to MS Word without using OLE&ComponentsFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 4.x (or higher)
Category:
VCL-General
Skill Level:
Scoring:
Last Update:
05/01/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet String Grid MS Word RTF report reporting quick export OLE automation
Times Scored:
3
Visits:
4013
Uploader: Eugene Kuchugurov
Company:
Reference: http://www.code.net.ru
 
Question/Problem/Abstract:
How to save objects to Word compatible format
Answer:



For exporting grid you may create Word compatible file in RTF format.
This is a simple example of saving string grid named StringGrid1 to file grid.rtf:



procedure WriteToStream(var Stream:TStream; s:string);
begin
Stream.Write(PChar(s)^,Length(s));
end;


procedure TForm1.ReportBtnClick(Sender: TObject);
var St:TStream;  f,r,cellwidth,cellpos:integer;
begin
St:=TFileStream.Create('grid.rtf',fmCreate);
try
//RTF header

WriteToStream(St,'{\rtf1\ansi\deff0\deflang1033');
WriteToStream(St,'{\fonttbl{\f0\fnil\fcharset1{\*\fname Arial;}Arial;}}');
WriteToStream(St,'\viewscale100\uc1\pard\f0\fs20\par');
cellwidth:=2988;
//Writing Grid Data
for r:=0 to StringGrid1.RowCount-1
do begin

WriteToStream(St,'{\trowd\trgaph70\trleft0\trrh230');
cellpos:=cellwidth;

for f:=0 to StringGrid1.ColCount-1 do begin
WriteToStream(St,'\clvertalt\clbrdrt\brdrs\brdrw10');
WriteToStream(St,'\clbrdrl\brdrs\brdrw10');
WriteToStream(St,'\clbrdrb\brdrs\brdrw10');
WriteToStream(St,'\clbrdrr\brdrs\brdrw10');
WriteToStream(St,'\cellx'+inttostr(cellpos));
cellpos:=cellpos+cellwidth;
end;

for f:=0 to StringGrid1.ColCount-1 do begin
WriteToStream(St,'\pard\plain\fs20\intbl ' + text2rtf(StringGrid1.Cells[f,r])+'\cell ');
end;
WriteToStream(St,'\row }');

end;//for r


//End of RTF file
WriteToStream(St,'\par }');
finally
if Assigned(St) then St.Free;
end;
showmessage('Export complete. Results are in file grid.rtf');
end;


This is function text2rtf used in procedure above:

function text2rtf(s:string):string;
const s2:string='';  i:integer=1;
begin
while i<=length(s) do begin
case ord(s[i]) of

92: s2:=s2+'\\';
123: s2:=s2+'\{';
125: s2:=s2+'\}';
128..255: s2:=s2+'\'''+inttohex(ord(s[i]),2);
else s2:=s2+s[i];

end;//case
inc(i);
end;
Result:=s2;
end;


This procedure creates Word compatible file without using OLE.
However, if you want to get a native Word document you may save a copy of file,
using OleVariant Word object:


procedure TForm1.DocBtnClick(Sender: TObject);
var V:OleVariant;
begin
try
V:=CreateOLEObject('Word.Application');
V.Documents.Open('grid.rtf');
V.ActiveDocument.SaveAs('grid.doc',0,False, '', True,'',False,False,False, False,False);
V.Quit(0);
V:=UnAssigned;
showmessage('Save complete');
except
showmessage('Could not save file document as doc');
end;
end;


See also my another article How to make reports in RTF format







Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Another alternative
    Keith Blows (May 2 2002 2:11PM)

I looked into just this thing a few years back and my initial reaction was also to use RTF. However, creating a HTML document with tables also works very nicely and there is less coding required.

Regards

Keith
Respond

RE: Another alternative
mostafa (Oct 5 2005 6:04PM)

I will b see it p p
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Kucherov
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)