Converter DBGrid em HTML

 

Nesta dica, vamos mostrar como transformar em um arquivo HTML, os dados de um DBGrid. Primeiramente, implemente as seguintes functions:

 

function ColorToHtml(mColor: TColor): string;

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

function DBGridToHtmlTable(mDBGrid: TDBGrid; mStrings: TStrings; mCaption: TCaption = ''): Boolean;

 

Implemente-os com o seguinte código:

 

function TForm1.ColorToHtml(mColor: TColor): string;

begin

  mColor := ColorToRGB(mColor);

  Result := Format('#%.2x%.2x%.2x', [GetRValue(mColor), GetGValue(mColor), GetBValue(mColor)]);

end;

 

function TForm1.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(mDBGrid.DataSource) then Exit;

  if not Assigned(mDBGrid.DataSource.DataSet) then Exit;

  if not mDBGrid.DataSource.DataSet.Active then Exit;

  vBookmark := mDBGrid.DataSource.DataSet.Bookmark;

  mDBGrid.DataSource.DataSet.DisableControls;

  try

    J := 0;

    vAllWidth := 0;

    for I := 0 to mDBGrid.Columns.Count - 1 do

      if mDBGrid.Columns[I].Visible then

      begin

        Inc(J);

        SetLength(vWidths, J);

        vWidths[J - 1] := mDBGrid.Columns[I].Width;

        Inc(vAllWidth, mDBGrid.Columns[I].Width);

      end;

    if J <= 0 then Exit;

    mStrings.Clear;

    mStrings.Add(Format('');

  finally

    mDBGrid.DataSource.DataSet.Bookmark := vBookmark;

    mDBGrid.DataSource.DataSet.EnableControls;

    vWidths := nil;

  end;

  Result := True;

end;

 

function TForm1.StrToHtml(mStr: string; mFont: TFont): string;

var

  vLeft, vRight: string;

begin

  Result := mStr;

  Result := StringReplace(Result, '&', '&', [rfReplaceAll]);

  Result := StringReplace(Result, '<', '<', [rfReplaceAll]);

  Result := StringReplace(Result, '>', '>', [rfReplaceAll]);

  if not Assigned(mFont) then Exit;

  vLeft := Format('',

    [mFont.Name, ColorToHtml(mFont.Color)]);

  vRight := '';

  if fsBold in mFont.Style then

  begin

    vLeft := vLeft + '';

    vRight := '' + vRight;

  end;

  if fsItalic in mFont.Style then

  begin

    vLeft := vLeft + '';

    vRight := '' + vRight;

  end;

  if fsUnderline in mFont.Style then

  begin

    vLeft := vLeft + '';

    vRight := '' + vRight;

  end;

  if fsStrikeOut in mFont.Style then

  begin

    vLeft := vLeft + '';

    vRight := '' + vRight;

  end;

  Result := vLeft + Result + vRight;

end;

 

Adicione no formulário um DataSource, um DBGrid, um ClientDataSet, Memo e um botão. Faça as ligações entre os componentes DataSource, DBGrid e ClientDataSet (utilize um arquivo XML). No OnClick do botão digite o seguinte código:

 

DBGridToHtmlTable(DBGrid1, Memo1.Lines, Caption);

Memo1.Lines.SaveToFile('c:\temp.htm');

ShellExecute(Handle, nil, 'c:\temp.htm', nil, nil, SW_SHOW);

 

Declare no uses ShellApi. Rode a aplicação e veja o resultado.

DBGrid.gif

 

Código:

Alexandre de Andrade Gonçalves

Analista/Programador

www.minasexport.com.br

www.cafemokaonline.com.br

 

Luciano Pimenta®

Editor do Portal ClubeDelphi.Net

webeditor@clubedelphi.net

www.clubedelphi.net