ComObj;
http://www.csdn.net/Expert/topicview.asp?id=576288

解决方案 »

  1.   

    ComObj:  CreateOleObject;
    ExcelApplication: Excel2000 or Excel97
      

  2.   

    如果不讲究定位和格式,这个函数就满足你的要求了。对于不同的Memo字段需要,请稍加修改,本函数将Memo中的回车符替换为#号。(为了读入时还原格式,如果你不需要,可直接将其用‘’替换)procedure ExportDataSetToCSVFile(const aDataSet:TDataSet;
      const aFileName:string);
    var aTextFile:TextFile;
        i:integer;
        aValue,LineStr:string;
        DataSetActive:boolean; 
    begin
      DataSetActive := aDataSet.Active; 
      if not aDataSet.Active then aDataSet.Open;
      aDataSet.DisableControls;
      try 
        aDataSet.FieldDefs.Update;
        if aDataSet.RecordCount=0 then exit; 
        try
          AssignFile(aTextFile,aFileName); 
          Rewrite(aTextFile); 
          LineStr := ''; 
          for i:=0 to aDataSet.FieldCount-1 do begin 
            aValue := aDataSet.Fields[i].DisplayName ; 
            if Pos(',',aValue) > 0 then aValue:='"' + aValue + '"'; 
            LineStr := LineStr + aValue + ','; 
          end; 
          Delete(LineStr,length(LineStr),1);// remove extra ',' 
          Writeln(aTextFile,LineStr); 
          aDataSet.First; 
          while not aDataSet.EOF do begin 
            LineStr := ''; 
            for i := 0 to aDataSet.FieldCount-1 do begin 
              if aDataSet.Fields[i].IsBlob then begin
                if  aDataSet.Fields[i].DataType=ftMemo then
                  aValue :=StringReplace(aDataSet.Fields[i].AsString,#13+#10,'#',[rfReplaceAll]);
              end else begin
                aValue := aDataSet.Fields[i].AsString;
              end; 
              if Pos(',',aValue) > 0 then aValue := '"' + aValue + '"'; 
              LineStr := LineStr+aValue+','; 
            end; 
            Delete(LineStr,length(LineStr),1);// remove extra ',' 
            Writeln(aTextFile,LineStr); 
            aDataSet.Next; 
          end;// while 
        finally 
          try 
            Flush(aTextFile); 
            CloseFile(aTextFile); 
          except 
          end; 
        end; 
      finally 
        aDataSet.EnableControls; 
        aDataset.Active  := DataSetActive; 
      end; 
    end;