如何将DBGrid的内容导出到HTML中(200分)

  • 主题发起人 主题发起人 evanszh
  • 开始时间 开始时间
E

evanszh

Unregistered / Unconfirmed
GUEST, unregistred user!
function ColorToHtml(mColor: TColor): string;<br>begin<br>&nbsp; Application.ProcessMessages;<br>&nbsp; mColor := ColorToRGB(mColor);<br>&nbsp; Result := Format('#%.2x%.2x%.2x', [GetRValue(mColor), GetGValue(mColor), GetBValue(mColor)]);<br>end; <br><br>function &nbsp;StrToHtml(mStr:string; mFont:TFont =nil):string;<br>var<br>&nbsp; vLeft, vRight: string;<br>begin<br>&nbsp; Application.ProcessMessages;<br>&nbsp; Result := mStr;<br>&nbsp; Result := StringReplace(Result, '&amp;', '&amp;AMP;', [rfReplaceAll]);<br>&nbsp; Result := StringReplace(Result, '&lt;', '&amp;LT;', [rfReplaceAll]);<br>&nbsp; Result := StringReplace(Result, '&gt;', '&amp;GT;', [rfReplaceAll]);<br><br>&nbsp; if Result = '' then Result := '-';<br>&nbsp; if not Assigned(mFont) then Exit;<br><br>&nbsp; vLeft := Format('&lt;FONT FACE="%s" COLOR="%s"&gt;', [mFont.Name, ColorToHtml(mFont.Color)]);<br>&nbsp; vRight := '&lt;/FONT&gt;';<br><br>&nbsp; if fsBold in mFont.Style then <br>&nbsp; begin<br>&nbsp; &nbsp; vLeft := vLeft + '&lt;B&gt;';<br>&nbsp; &nbsp; vRight := '&lt;/B&gt;' + vRight;<br>&nbsp; end;<br><br>&nbsp; if fsItalic in mFont.Style then<br>&nbsp; begin<br>&nbsp; &nbsp; vLeft := vLeft + '&lt;I&gt;';<br>&nbsp; &nbsp; vRight := '&lt;/I&gt;' + vRight;<br>&nbsp; end;<br><br>&nbsp; if fsUnderline in mFont.Style then<br>&nbsp; begin<br>&nbsp; &nbsp; vLeft := vLeft + '&lt;U&gt;';<br>&nbsp; &nbsp; vRight := '&lt;/U&gt;' + vRight;<br>&nbsp; end;<br><br>&nbsp; if fsStrikeOut in mFont.Style then<br>&nbsp; begin<br>&nbsp; &nbsp; vLeft := vLeft + '&lt;S&gt;';<br>&nbsp; &nbsp; vRight := '&lt;/S&gt;' + vRight;<br>&nbsp; end;<br><br>&nbsp; Result := vLeft + Result + vRight;<br>end; <br><br>function DBGridToHtml(mDBGrid: TDBGrid; mStrings: TStrings; mCaption: TCaption = 'HTML文件的标题'): Boolean;<br>const<br>&nbsp; cAlignText: array[TAlignment] of string = ('LEFT', 'RIGHT', 'CENTER');<br><br>var<br>&nbsp; vColFormat, vColText: string;<br>&nbsp; vAllWidth: Integer;<br>&nbsp; vWidths: array of Integer;<br>&nbsp; vBookmark: string;<br>&nbsp; I, J: Integer;<br>begin<br>&nbsp; Application.ProcessMessages;<br>&nbsp; Result := False;<br>&nbsp; if not Assigned(mStrings) then Exit;<br>&nbsp; if not Assigned(mDBGrid) then Exit;<br>&nbsp; if not Assigned(mDBGrid.DataSource) then Exit;<br>&nbsp; if not Assigned(mDBGrid.DataSource.DataSet) then Exit;<br>&nbsp; if not mDBGrid.DataSource.DataSet.Active then Exit;<br><br>&nbsp; vBookmark := mDBGrid.DataSource.DataSet.Bookmark;<br>&nbsp; mDBGrid.DataSource.DataSet.DisableControls;<br>&nbsp; try<br>&nbsp; &nbsp; J := 0;<br>&nbsp; &nbsp; vAllWidth := 0;<br>&nbsp; &nbsp; for I := 0 to mDBGrid.Columns.Count - 1 do<br>&nbsp; &nbsp; &nbsp; if mDBGrid.Columns.Visible then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(J);<br>&nbsp; &nbsp; &nbsp; &nbsp; SetLength(vWidths, J);<br>&nbsp; &nbsp; &nbsp; &nbsp; vWidths[J - 1] := mDBGrid.Columns.Width;<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(vAllWidth, mDBGrid.Columns.Width);<br>&nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; if J &lt;= 0 then Exit;<br>&nbsp; &nbsp; mStrings.Clear;<br>&nbsp; &nbsp; mStrings.Add(Format('&lt;TABLE BGCOLOR="%s" BORDER=1 WIDTH="100%%"&gt;', [ColorToHtml(mDBGrid.Color)]));<br>&nbsp; &nbsp; if mCaption&lt;&gt; '' then mStrings.Add(Format('&lt;CAPTION&gt;%s&lt;/CAPTION&gt;', [StrToHtml(mCaption)]));<br>&nbsp; &nbsp; vColFormat := '';<br>&nbsp; &nbsp; vColText := '';<br>&nbsp; &nbsp; vColFormat := vColFormat + '&lt;TR&gt;' + #13#10;<br>&nbsp; &nbsp; vColText := vColText + '&lt;TR&gt;' + #13#10;<br>&nbsp; &nbsp; J := 0;<br><br>&nbsp; &nbsp; for I := 0 to mDBGrid.Columns.Count - 1 do<br>&nbsp; &nbsp; &nbsp; if mDBGrid.Columns.Visible then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; vColFormat := vColFormat + Format(' &lt;TD BGCOLOR="%s" ALIGN=%s WIDTH="%d%%"&gt;DisplayText%d&lt;/TD&gt;' + #13#10, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[ColorToHtml(mDBGrid.Columns.Color), cAlignText[mDBGrid.Columns.Alignment], <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Round(vWidths[J] / vAllWidth * 100), J]);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; vColText := vColText + Format(' &lt;TD BGCOLOR="%s" ALIGN=%s WIDTH="%d%%"&gt;%s&lt;/TD&gt;' + #13#10,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [ColorToHtml(mDBGrid.Columns.Title.Color), cAlignText[mDBGrid.Columns.Alignment], <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Round(vWidths[J] / vAllWidth * 100), <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StrToHtml(mDBGrid.Columns.Title.Caption, mDBGrid.Columns.Title.Font)]);<br>&nbsp; &nbsp; &nbsp; &nbsp; Inc(J);<br>&nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; vColFormat := vColFormat + '&lt;/TR&gt;' + #13#10;<br>&nbsp; &nbsp; vColText := vColText + '&lt;/TR&gt;' + #13#10;<br>&nbsp; &nbsp; mStrings.Text := mStrings.Text + vColText;<br><br>&nbsp; &nbsp; mDBGrid.DataSource.DataSet.First;<br>&nbsp; &nbsp; while not mDBGrid.DataSource.DataSet.Eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; J := 0;<br>&nbsp; &nbsp; &nbsp; vColText := vColFormat;<br>&nbsp; &nbsp; &nbsp; for I := 0 to mDBGrid.Columns.Count - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; if mDBGrid.Columns.Visible then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vColText := StringReplace(vColText, Format('&gt;DisplayText%d&lt;', [J]), Format('&gt;%s&lt;', <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [StrToHtml(mDBGrid.Columns.Field.DisplayText, mDBGrid.Columns.Font)]), [rfReplaceAll]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(J);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; mStrings.Text := mStrings.Text + vColText;<br>&nbsp; &nbsp; &nbsp; mDBGrid.DataSource.DataSet.Next;<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; mStrings.Add('&lt;/TABLE&gt;');<br>&nbsp; finally<br>&nbsp; &nbsp; mDBGrid.DataSource.DataSet.Bookmark := vBookmark;<br>&nbsp; &nbsp; mDBGrid.DataSource.DataSet.EnableControls;<br>&nbsp; &nbsp; vWidths := nil;<br>&nbsp; end;<br><br>&nbsp; Result := True;<br>end;
 
接受答案了.
 
后退
顶部