一个DataSet to Excel的函数

function DS2XLS(DS:TDataSet;FS:string):Boolean;
var
  i,j:Integer;
  sl:TStringList;
  str:String;
begin
  try
  sl:=TStringList.Create();
    try
      DS.First;

      sl.Add('<html> <table width="100%"  border="1">');

      for i:=0 to DS.FieldCount-1 do
          str:=str+'<td>'+DS.Fields[i].DisplayName+'</td>';
      sl.Add(str);

      for j:=0 to DS.RecordCount-1 do
      begin
        str:='<tr>';
        for i:=0 to DS.FieldCount-1 do
        if  varisnull(DS.Fields[i].Value) then  str:=str+'<td>'+'</td>' else  str:=str+'<td>'+string(DS.Fields[i].Value)+'</td>';
        str:=str+'</tr>';
        sl.Add(str);
        DS.Next;
      end;
      sl.Add('</table></html>');

      sl.SaveToFile(FS);
      Result:=True;
    except
      REsult:=False;
    end;
  finally
    sl.Free;
  end;
end;

你可能感兴趣的:(Excel)