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