0% found this document useful (0 votes)
8 views3 pages

Dbgrid em HTML

The document contains a code snippet for converting a DBGrid to an HTML table in Delphi. It includes functions for color conversion, string formatting, and generating HTML table structure based on the DBGrid's properties. Additionally, an example procedure demonstrates how to use the conversion function and save the output to an HTML file.

Uploaded by

Daniel Rubies
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Dbgrid em HTML

The document contains a code snippet for converting a DBGrid to an HTML table in Delphi. It includes functions for color conversion, string formatting, and generating HTML table structure based on the DBGrid's properties. Additionally, an example procedure demonstrates how to use the conversion function and save the output to an HTML file.

Uploaded by

Daniel Rubies
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

800 - Converter DBGrig em Html

///////Inicio do Código

function ColorToHtml(mColor: TColor): string;


begin
mColor := ColorToRGB(mColor);
Result := Format('#%.2x%.2x%.2x',
[GetRValue(mColor), GetGValue(mColor), GetBValue(mColor)]);
end; { ColorToHtml }

function StrToHtml(mStr: string; mFont: TFont = nil): string;


var
vLeft, vRight: string;
begin
Result := mStr;
Result := StringReplace(Result, '&', '&', [rfReplaceAll]);
Result := StringReplace(Result, '<', '&LT;', [rfReplaceAll]);
Result := StringReplace(Result, '>', '&GT;', [rfReplaceAll]);
if not Assigned(mFont) then Exit;
vLeft := Format('<FONT FACE="%s" COLOR="%s">',
[[Link], ColorToHtml([Link])]);
vRight := '</FONT>';
if fsBold in [Link] then begin
vLeft := vLeft + '<B>';
vRight := '</B>' + vRight;
end;
if fsItalic in [Link] then begin
vLeft := vLeft + '<I>';
vRight := '</I>' + vRight;
end;
if fsUnderline in [Link] then begin
vLeft := vLeft + '<U>';
vRight := '</U>' + vRight;
end;
if fsStrikeOut in [Link] then begin
vLeft := vLeft + '<S>';
vRight := '</S>' + vRight;
end;
Result := vLeft + Result + vRight;
end; { StrToHtml }

function DBGridToHtmlTable(mDBGrid: TDBGrid; mStrings: TStrings;


mCaption: TCaption = ''): Boolean;
const
cAlignText: array[TAlignment] of string = ('LEFT', 'RIGHT', 'CENTER');
var
vColFormat: string;
vColText: string;
vAllWidth: Integer;
vWidths: array of Integer;
vBookmark: string;
I, J: Integer;
begin
Result := False;
if not Assigned(mStrings) then Exit;
if not Assigned(mDBGrid) then Exit;
if not Assigned([Link]) then Exit;
if not Assigned([Link]) then Exit;
if not [Link] then Exit;
vBookmark := [Link];
[Link];
try
J := 0;
vAllWidth := 0;
for I := 0 to [Link] - 1 do
if [Link][I].Visible then begin
Inc(J);
SetLength(vWidths, J);
vWidths[J - 1] := [Link][I].Width;
Inc(vAllWidth, [Link][I].Width);
end;
if J <= 0 then Exit;
[Link];
[Link](Format('<TABLE BGCOLOR="%s" BORDER=1 WIDTH="100%%">',
[ColorToHtml([Link])]));
if mCaption <> '' then
[Link](Format('<CAPTION>%s</CAPTION>', [StrToHtml(mCaption)]));
vColFormat := '';
vColText := '';
vColFormat := vColFormat + '<TR>'#13#10;
vColText := vColText + '<TR>'#13#10;
J := 0;
for I := 0 to [Link] - 1 do
if [Link][I].Visible then begin
vColFormat := vColFormat + Format(
' <TD BGCOLOR="%s" ALIGN=%s WIDTH="%d%%">DisplayText%d</TD>'#13#10,
[ColorToHtml([Link][I].Color),
cAlignText[[Link][I].Alignment],
Round(vWidths[J] / vAllWidth * 100), J]);
vColText := vColText + Format(
' <TD BGCOLOR="%s" ALIGN=%s WIDTH="%d%%">%s</TD>'#13#10,
[ColorToHtml([Link][I].[Link]),
cAlignText[[Link][I].Alignment],
Round(vWidths[J] / vAllWidth * 100),
StrToHtml([Link][I].[Link],
[Link][I].[Link])]);
Inc(J);
end;
vColFormat := vColFormat + '</TR>'#13#10;
vColText := vColText + '</TR>'#13#10;
[Link] := [Link] + vColText;
[Link];
while not [Link] do begin
J := 0;
vColText := vColFormat;
for I := 0 to [Link] - 1 do
if [Link][I].Visible then begin
vColText := StringReplace(vColText, Format('>DisplayText%d<', [J]),
Format('>%s<', [StrToHtml([Link][I].[Link],
[Link][I].Font)]),
[rfReplaceAll]);
Inc(J);
end;
[Link] := [Link] + vColText;
[Link];
end;
[Link]('</TABLE>');
finally
[Link] := vBookmark;
[Link];
vWidths := nil;
end;
Result := True;
end; { DBGridToHtmlTable }

///////Fim do Código

{ uses ShellApi; }

///////Inicio do Exemplo

procedure TForm1.Button1Click(Sender: TObject);


begin
DBGridToHtmlTable(DBGrid1, [Link], Caption);
[Link]('c:\[Link]');
ShellExecute(Handle, nil, 'c:\[Link]', nil, nil, SW_SHOW);
end;

///////Fim do Exemplo

Alexandre de Andrade Gonçalves

Analista/Programador

[Link]

[Link]

You might also like