Here some code I used last week to generate html from a GridView. I use it to display output in an EmbeddedWB component (found at http://www.euromind.com). It turns out that with some effort this internet explorer wrapper is a very usable tool for print previewing and printing (as you let IE do the 'dirty' work).
CODE
function InsertGridViewTable(const GridView: TGridView) : string;
var
r, c : Integer;
sl : TStringList;
begin
sl:=TStringList.Create;
sl.Add('<TABLE BORDER CELLSPACING=0 CELLPADDING=1 WIDTH="100%">');
sl.Add('<TR ALIGN="left" VALIGN="middle">');
for c := 0 to Pred(GridView.Columns.Count) do
sl.Add(Format(' <TH>%s</TH>', [GridView.Columns[c].Header.Caption]));
sl.Add('</TR>');
for r := 0 to Pred(GridView.RowCount) do
begin
sl.Add('<TR ALIGN="left" VALIGN="middle">');
for c := 0 to Pred(GridView.Columns.Count) do
begin
if (GridView.Columns.Item[c] is TNumericColumn) then
sl.Add(Format(' <TD ALIGN="right">%s</TD>', [GridView.Cells[c, r]]))
else
sl.Add(Format(' <TD>%s</TD>', [GridView.Cells[c, r]]));
end;
sl.Add('</TR>');
end;
sl.Add('</TABLE>');
Result:=sl;
sl.Free;
end;
var
r, c : Integer;
sl : TStringList;
begin
sl:=TStringList.Create;
sl.Add('<TABLE BORDER CELLSPACING=0 CELLPADDING=1 WIDTH="100%">');
sl.Add('<TR ALIGN="left" VALIGN="middle">');
for c := 0 to Pred(GridView.Columns.Count) do
sl.Add(Format(' <TH>%s</TH>', [GridView.Columns[c].Header.Caption]));
sl.Add('</TR>');
for r := 0 to Pred(GridView.RowCount) do
begin
sl.Add('<TR ALIGN="left" VALIGN="middle">');
for c := 0 to Pred(GridView.Columns.Count) do
begin
if (GridView.Columns.Item[c] is TNumericColumn) then
sl.Add(Format(' <TD ALIGN="right">%s</TD>', [GridView.Cells[c, r]]))
else
sl.Add(Format(' <TD>%s</TD>', [GridView.Cells[c, r]]));
end;
sl.Add('</TR>');
end;
sl.Add('</TABLE>');
Result:=sl;
sl.Free;
end;
If you add the following html code to the HEAD section you also get thin lines in the table.
CODE
<STYLE>
TABLE {border-collapse:collapse}
TD {border-width:1px}
TH {background-color:silver}
</STYLE>
TABLE {border-collapse:collapse}
TD {border-width:1px}
TH {background-color:silver}
</STYLE>
This last coding with thanks to my colleague Francis who is really good in stylesheets.
As always have fun coding (and may the force be with you











