DBGrid变换成HTML表格

本文介绍了一种将Delphi中的DBGrid组件转换为HTML表格的方法,包括颜色、字体样式及表格结构的转换。通过自定义函数实现对每个单元格样式的处理,并完整地展示了从数据集读取数据到生成HTML文件的过程。
摘要由CSDN通过智能技术生成
DBGrid变换成HTML表格
提交日期:2003-8-9 
关键词:DBGrid HTML 

///Begin Source

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 Result ='' then Result:='-';

if not Assigned(mFont) then Exit;

vLeft := Format('<FONT FACE="%s" COLOR="%s">',

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

vRight := '</FONT>';

if fsBold in mFont.Style then begin

vLeft := vLeft + '<B>';

vRight := '</B>' + vRight;

end;

if fsItalic in mFont.Style then begin

vLeft := vLeft + '<I>';

vRight := '</I>' + vRight;

end;

if fsUnderline in mFont.Style then begin

vLeft := vLeft + '<U>';

vRight := '</U>' + vRight;

end;

if fsStrikeOut in mFont.Style 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(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('<TABLE BGCOLOR="%s" BORDER=1 WIDTH="100%%">',

[ColorToHtml(mDBGrid.Color)]));

if mCaption <> '' then

mStrings.Add(Format('<CAPTION>%s</CAPTION>', [StrToHtml(mCaption)]));

vColFormat := '';

vColText := '';

vColFormat := vColFormat + '<TR>'#13#10;

vColText := vColText + '<TR>'#13#10;

J := 0;

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

if mDBGrid.Columns[I].Visible then begin

vColFormat := vColFormat + Format(

' <TD BGCOLOR="%s" ALIGN=%s WIDTH="%d%%">DisplayText%d</TD>'#13#10,

[ColorToHtml(mDBGrid.Columns[I].Color),

cAlignText[mDBGrid.Columns[I].Alignment],

Round(vWidths[J] / vAllWidth * 100), J]);

vColText := vColText + Format(

' <TD BGCOLOR="%s" ALIGN=%s WIDTH="%d%%">%s</TD>'#13#10,

[ColorToHtml(mDBGrid.Columns[I].Title.Color),

cAlignText[mDBGrid.Columns[I].Alignment],

Round(vWidths[J] / vAllWidth * 100),

StrToHtml(mDBGrid.Columns[I].Title.Caption,

mDBGrid.Columns[I].Title.Font)]);

Inc(J);

end;

vColFormat := vColFormat + '</TR>'#13#10;

vColText := vColText + '</TR>'#13#10;

mStrings.Text := mStrings.Text + vColText;

mDBGrid.DataSource.DataSet.First;

while not mDBGrid.DataSource.DataSet.Eof do begin

J := 0;

vColText := vColFormat;

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

if mDBGrid.Columns[I].Visible then begin

vColText := StringReplace(vColText, Format('>DisplayText%d<', [J]),

Format('>%s<', [StrToHtml(mDBGrid.Columns[I].Field.DisplayText,

mDBGrid.Columns[I].Font)]),

[rfReplaceAll]);

Inc(J);

end;

mStrings.Text := mStrings.Text + vColText;

mDBGrid.DataSource.DataSet.Next;

end;

mStrings.Add('</TABLE>');

finally

mDBGrid.DataSource.DataSet.Bookmark := vBookmark;

mDBGrid.DataSource.DataSet.EnableControls;

vWidths := nil;

end;

Result := True;

end; { DBGridToHtmlTable }

///End Source

{ uses ShellApi; }

///Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

DBGridToHtmlTable(DBGrid1, Memo1.Lines, Caption);

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

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

end;

///End Demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值