2005. január 7., péntek

String Grid to HTML file

Problem/Question/Abstract:

Procedure to dump the contents of a TStringGrid to HTML format file.

StrGridToHTML(const FileName: string;
StrGrid: TStringGrid;
const Heading: string = '';
TextColor: TColor = clBlack;
TableBgColor: TColor = clAqua);

Arguments

FileName : String containing Path and Filename of output file.
StrGrid : TStringGrid component to dup to file.
Heading : HTML Table Header Text. (optional default = nullstr)
TextColor : TColor denoting color of HTML table text (default = clblack)
TableBgColor : TColor denoting background color of HTML table (default = clAqua)

Example of Syntax

StrGridToHTML('c:\temp\test.htm', StringGrid1, 'Account Code', clBlue, clWhite);

Answer:

procedure StrGridToHTML(const FileName: string;
StrGrid: TStringGrid;
const Heading: string = '';
TextColor: TColor = clBlack;
TableBgColor: TColor = clAqua);
var
Txt: TextFile;
i, ii: integer;
BgColor, TxColor: string;
begin
// Convert TColor to HTML Hex Color
BgColor := IntToHex(GetRValue(TableBgColor), 2) +
IntToHex(GetGValue(TableBgColor), 2) +
IntToHex(GetBValue(TableBgColor), 2);

TxColor := IntToHex(GetRValue(TextColor), 2) +
IntToHex(GetGValue(TextColor), 2) +
IntToHex(GetBValue(TextColor), 2);

// Create output file
AssignFile(Txt, FileName);
Rewrite(Txt);

// HTML Header Info
WriteLn(Txt, '<HTML>');
WriteLn(Txt, '<HEAD>');
WriteLn(Txt, '<TITLE>' + ExtractFileName(FileName) + '</TITLE>');
WriteLn(Txt, '</HEAD>');
WriteLn(Txt);
WriteLn(Txt, '<BODY TEXT=#' + TxColor + ' BGCOLOR=#CCCCCC>');
WriteLn(Txt, '<H1>' + Heading + '</H1>');
WriteLn(Txt, '<TABLE WIDTH=100% CELLPADDING=2 CELLSPACING=2 ' +
'BGCOLOR=#' + BgColor + ' BORDER=1>');

// Column Descriptions
WriteLn(Txt, '    <TR>');
for i := 0 to StrGrid.ColCount - 1 do
WriteLn(Txt, '        <TH>' + StrGrid.Cells[i, 0] + '</TH>');
WriteLn(Txt, '    </TR>');

// Write out the Grid Data
for i := 1 to StrGrid.RowCount - 1 do
begin
WriteLn(Txt, '    <TR>');
for ii := 0 to StrGrid.ColCount - 1 do
WriteLn(Txt, '    <TD>' + StrGrid.Cells[ii, i] + '</TD>');
WriteLn(Txt, '    </TR>');
end;

// Footer
WriteLn(Txt, '<TABLE>');
WriteLn(Txt, '<P>');
WriteLn(Txt, '<H3>' + IntToStr(StrGrid.ColCount) + ' Rows</H3>');
WriteLn(Txt, '</BODY>');
WriteLn(Txt, '</HTML>');

CloseFile(Txt);
end;


Nincsenek megjegyzések:

Megjegyzés küldése