Author: Igor Siticov
How to use MS Word as report generator?
Answer:
Why not use the MS Word as report generator in your projects? We can easyly build
the report and allow user to modify it using well known editor in any way he wants.
The example below demonstartes how to build the report based on StringGrid
contents.
1 procedure TsiGridReporter.ShowReport;
2 var
3 Range: Variant;
4 i, j: integer;
5 begin
6 if FGrid = nil then
7 raise Exception.Create('No grid selected!');
8 try
9 FWordApp := CreateOleObject('Word.Application');
10 except
11 raise Exception.Create('Cannot start MS Word!');
12 end;
13 FWordApp.Visible := True;
14 FWordApp.Documents.Add;
15 if FShowDate then
16 begin
17 Range := FWordApp.Documents.Item(1);
18 Range := Range.Sections.Item(1);
19 Range := Range.Headers.Item(1).Range;
20 Range.Text := 'Date: ' + DateToStr(Date) + ' Time: ' + TimeToStr(Time);
21 end;
22 Range := FWordApp.Documents.Item(1);
23 Range := Range.Sections.Item(1);
24 Range := Range.Footers.Item(1);
25 Range.Range.Text := 'Page:';
26 Range.Range.ParagraphFormat.Alignment := ord(waAlignParagraphRight);
27 Range.PageNumbers.Add;
28 FWordApp.Documents.Item(1).Paragraphs.Add;
29 Range := FWordApp.Documents.Item(1).Range(
30
31 FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Cou
32 nt - 1).Range.end,
33
34 FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Cou
35 nt - 1).Range.end);
36 Range.Text := FTitle;
37 Range.Bold := fsBold in FTitleFont.Style;
38 Range.Italic := fsItalic in FTitleFont.Style;
39 Range.Underline := fsUnderline in FTitleFont.Style;
40 Range.Font.StrikeThrough := fsStrikeOut in FTitleFont.Style;
41 Range.Font.Name := FTitleFont.Name;
42 Range.Font.Size := FTitleFont.Size;
43 Range.Font.ColorIndex := ord(FTitleColor);
44 Range.ParagraphFormat.Alignment := ord(FTitleAlignment);
45 FWordApp.Documents.Item(1).Paragraphs.Add;
46 FWordApp.Documents.Item(1).Paragraphs.Add;
47 Range := FWordApp.Documents.Item(1).Range(
48
49 FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Cou
50 nt - 1).Range.end,
51
52 FWordApp.Documents.Item(1).Paragraphs.Item(FWordApp.Documents.Item(1).Paragraphs.Cou
53 nt - 1).Range.end);
54 FWordApp.Documents.Item(1).Tables.Add(Range, FGrid.RowCount, FGrid.ColCount);
55 Range :=
56 FWordApp.Documents.Item(1).Tables.Item(FWordApp.Documents.Item(1).Tables.Count);
57 for i := 1 to FGrid.RowCount do
58 for j := 1 to FGrid.ColCount do
59 begin
60 Range.Cell(i, j).Range.InsertAfter(FGrid.Cells[j - 1, i - 1]);
61 if (i <= FGrid.FixedRows) or (j <= FGrid.FixedCols) then
62 begin
63 Range.Cell(i, j).Range.Bold := True;
64 Range.Cell(i, j).Range.Shading.BackgroundPatternColorIndex := ord(wcGray25);
65 end
66 else
67 begin
68 Range.Cell(i, j).Range.Bold := fsBold in FCellFont.Style;
69 Range.Cell(i, j).Range.Italic := fsItalic in FCellFont.Style;
70 Range.Cell(i, j).Range.Underline := fsUnderline in FCellFont.Style;
71 Range.Cell(i, j).Range.Font.StrikeThrough := fsStrikeOut in FCellFont.Style;
72 Range.Cell(i, j).Range.Font.Name := FCellFont.Name;
73 Range.Cell(i, j).Range.Font.Size := FCellFont.Size;
74 // Range.Cell(i, j).Range.Font.ColorIndex := ord(FCellColor);
75 Range.Cell(i, j).Range.Shading.BackgroundPatternColorIndex := FCellColor;
76 end;
77 end;
78 end;
This example is just one method of component attached to this article. This
component also has the PrintReport and PrintPreview methods.
See attached source code for details. This component could give you just the first
step for creating your own full featured report generator based on using MS Word.
P.S. Component and source code are FREEWARE, so you can use it as you want.
Component Download: http://www.sicomponents.com/soft/sireport.zip
|