Mirror

String Grid to HTML file (Views: 100)

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, '');
WriteLn(Txt, '');
WriteLn(Txt, '' + ExtractFileName(FileName) + '');
WriteLn(Txt, '');
WriteLn(Txt);
WriteLn(Txt, '');
WriteLn(Txt, '

' + Heading + '

');
WriteLn(Txt, ''BGCOLOR=#' + BgColor + ' BORDER=1>');

// Column Descriptions
WriteLn(Txt, '    ');
for i := 0 to StrGrid.ColCount - 1 do
WriteLn(Txt, '        ');
WriteLn(Txt, '    ');

// Write out the Grid Data
for i := 1 to StrGrid.RowCount - 1 do
begin
WriteLn(Txt, '    ');
for ii := 0 to StrGrid.ColCount - 1 do
WriteLn(Txt, '    ');
WriteLn(Txt, '    ');
end;

// Footer
WriteLn(Txt, '
' + StrGrid.Cells[i, 0] + '
' + StrGrid.Cells[ii, i] + '
');
WriteLn(Txt, '

');
WriteLn(Txt, '

' + IntToStr(StrGrid.ColCount) + ' Rows

');
WriteLn(Txt, '');
WriteLn(Txt, '');

CloseFile(Txt);
end;


<< Back to main page