Advertisement
filhotecmail

HtmlTable

Sep 6th, 2020
2,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.09 KB | None | 0 0
  1. var
  2.   shtml     : widestring;
  3.   htmlfile  : TextFile;
  4.   i         : integer;
  5.   AvailableFields: set of  TFieldType;
  6.   bookmark : TBookmark;
  7. begin
  8.   Result:= Self;
  9.   bookmark := dataset.Bookmark;
  10.   DataSet.DisableControls;
  11.   DataSet.First;
  12.   AvailableFields:=[ftInteger, ftWideString, ftDate,  ftBcd, ftmemo, ftstring];
  13. // --> cria o código html da página
  14.   shtml:= '<html> <head>'+#13#10;
  15.   shtml:= shtml + '<title>'+dataset.Name+'</title></head>' + #13#10;
  16.   shtml:= shtml + '<body>' + #13#10;
  17.   shtml:= shtml + '<script>'+#13+'function cor_tabela(id){'+#13+
  18.                   'var cor1="#dae5e3";'+#13+
  19.                   'var cor2="#fff";'+#13+
  20.                   'var x=document.getElementById(id).getElementsByTagName("tr");'+#13+
  21.                   'for(i=0;i<x.length;i++)'+#13+
  22.                   'x[i].style.backgroundColor=(i%2==0)?cor1:cor2;'+#13+
  23.                   '}'+#13+
  24.                   '</script>'+#13+#10;
  25.   shtml:= shtml + '<style>'+#13+
  26.                   'table {'+#13+
  27.                         'border:solid 1px #000;'+#13+
  28.                         'font-family:Verdana, Geneva, sans-serif;'+#13+
  29.                         'font-size:11px;'+#13+
  30.                         'background:#930;'+#13+
  31.                   '}'+#13+
  32.                   'th {'+#13+
  33.                         'color:#FFF;'+#13+
  34.                         'background:#930;'+#13+
  35.                         'height:25px;'+#13+
  36.                         'vertical-align:middle;'+#13+
  37.                   '}'+#13+
  38.                   '</style>'+#13+#10;
  39.   //header da tabela
  40.   shtml:= shtml + '<table id="tb" cellspacing="1" width="100%">' + #13#10;
  41.   shtml:= shtml + '<tr>' + #13#10;
  42.   for i:=0 to DataSet.FieldCount-1 do
  43.   begin
  44.     if DataSet.Fields[i].DataType in AvailableFields  then
  45.     begin
  46.       shtml:= shtml + '<th>';
  47.       shtml:= shtml +
  48.               '' +
  49.               DataSet.Fields.Fields[i].DisplayName +
  50.               '';
  51.       shtml:= shtml + '</th>' + #13#10;
  52.     end;
  53.   end;{for}
  54.   shtml:= shtml + '</tr>' + #13#10;
  55.  
  56.   //corpo da tabela
  57.   while not DataSet.Eof do
  58.   begin
  59.     shtml:= shtml + '<tr>' + #13#10;
  60.     for i:=0 to DataSet.FieldCount-1 do
  61.     begin
  62.       if DataSet.Fields.Fields[i].DataType in AvailableFields then
  63.       begin
  64.         if DataSet.Fields.Fields[i].DataType = ftBcd then
  65.         begin
  66.           shtml:= shtml + '<td align="right">'+ formatfloat('R$ ###,##0.00', DataSet.Fields.Fields[i].AsCurrency);
  67.         end else
  68.         begin
  69.           shtml:= shtml + '<td>'+DataSet.Fields.Fields[i].AsString;
  70.           shtml:= shtml + '</td>' + #13#10;
  71.         end;
  72.       end;
  73.     end;{for}
  74.     shtml:= shtml + '</tr>' + #13#10;
  75.     DataSet.Next;
  76.   end;{while}
  77.   shtml:= shtml + '</table>' + #13#10;
  78.   //script para zebrar a tabela
  79.   shtml:= shtml + '<script>cor_tabela("tb");</script>'+#13+#10;
  80.   //finalizando a página
  81.   shtml:= shtml + '</body></html>';
  82. // --> salva o arquivo html
  83.   AssignFile(htmlfile,ChangeFileExt(Arq,'.'+Ext));
  84.   Rewrite(htmlfile);
  85.   WriteLn(htmlfile, shtml);
  86.   CloseFile(htmlfile);
  87.   DataSet.Bookmark := bookmark;
  88.   DataSet.EnableControls;
  89.  
  90. end;
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement