我想把Access数据库里面的某些记录导出到某个txt文档里面,然后再导入SQL,请问用什么方式实现最方便啊?

解决方案 »

  1.   

    直接导出成excel再导入SQL Server也很方便
      

  2.   

    to WWWWA(aaaa)
      可不可以详细点说啊?to google1106
       我是菜鸟,在delphi里面怎样把数据导入excel啊? 
      

  3.   

    //导出文本文件
    Function DataSetToTXT(DataSet:TDataSet;FileName:String):Boolean;
    var
      s:TStringList;
      str:string;
      i:integer;
      SaveDialog:TSaveDialog;
      BK:TBookMark;
    begin
         s:=TStringList.Create;
         BK:=DataSet.GetBookMark;
         DataSet.DisableControls;
         DataSet.First;
         while not DataSet.Eof do
         begin
             str:='';
             for i:=0 to DataSet.FieldCount-1 do
             begin
                 if str='' then
                   str:=str+DataSet.Fields[i].AsString
                 else str:=str+'|'+DataSet.Fields[i].AsString;
             end;
             s.Add(str);
             DataSet.Next;
         end;
         DataSet.GotoBookMark(BK);
         DataSet.EnableControls;
         SaveDialog:=TSaveDialog.Create(Nil);
         SaveDialog.Filter:='文本文件(*.txt)|*.txt';
         SaveDialog.Title:='将'+FileName+'数据表导出文本文件';
         SaveDialog.FileName:=FileName+'.txt';
         Result:=SaveDialog.Execute;
         UpdateWindow(GetActiveWindow);
         if Result then
           s.SaveToFile(SaveDialog.FileName);
         SaveDialog.Free;
         s.Free;
    end;
    //导出Excel
    Function DataSetToExcelSheet(DataSet:TDataSet;FieldTagMax:Integer;Sheet:OleVariant):Boolean;
    var
      Row,Col,FieldIndex:Integer;
      BK:TBookMark;
    begin
        Result := False;
        if not Dataset.Active then exit;
        BK:=DataSet.GetBookMark;
        DataSet.DisableControls;
        Sheet.Activate;
        try
          // 列标题
          Row:=1;
          Col:=1;
          for FieldIndex:=0 to DataSet.FieldCount-1 do
          begin
              if DataSet.Fields[FieldIndex].Tag<=FieldTagMax then
                begin
                    Sheet.Cells(Row,Col):=DataSet.Fields[FieldIndex].DisplayLabel;
                    Inc(Col);
                end;
          end;
          // 表内容
          DataSet.First;
          while Not DataSet.Eof do
          begin
              Row:=Row+1;
              Col:=1;
              for FieldIndex:=0 to DataSet.FieldCount-1 do
              begin
                  if DataSet.Fields[FieldIndex].Tag<=FieldTagMax then
                    begin
                        Sheet.Cells(Row,Col):=DataSet.Fields[FieldIndex].AsString;
                        Inc(Col);
                    end;
              end;
              DataSet.Next;
          end;
          Result:=True;
        finally
          DataSet.GotoBookMark(BK);
          DataSet.EnableControls;
        end;
    end;Function DataSetToExcel(DataSet:TDataSet;FieldTagMax:Integer;Visible:Boolean;ExcelFileName:String='';FileName:String=''):Boolean;
    var
      ExcelObj,Excel,WorkBook,Sheet:OleVariant;
      OldCursor:TCursor;
      SaveDialog:TSaveDialog;
    begin
        Result:=False;
        if not Dataset.Active then exit;
        OldCursor:=Screen.Cursor;
        Screen.Cursor:=crHourGlass;
        try
          ExcelObj:=CreateOleObject('Excel.Sheet');
          Excel:=ExcelObj.Application;
          Excel.Visible:=Visible;
          WorkBook:=Excel.Workbooks.Add;
          Sheet:=WorkBook.Sheets[1];
        except
          MessageBox(GetActiveWindow,'无法调用Mircorsoft Excel! '+chr(13)+chr(10)+'请检查是否安装了Mircorsoft Excel!','提示',MB_OK+MB_ICONINFORMATION);
          Screen.Cursor:=OldCursor;
          Exit;
        end;
        Result:=DataSetToExcelSheet(DataSet,FieldTagMax,Sheet);
        if Result then
          if Not Visible then
            begin
                if ExcelFileName<>'' then
                  WorkBook.SaveAs(FileName:=ExcelFileName)
                else
                begin
                    SaveDialog:=TSaveDialog.Create(Nil);
                    SaveDialog.Filter:='Microsoft Excel 文件(*.xls)|*.xls';
                    SaveDialog.Title:='将'+FileName+'数据表导出Excel文件';
                    SaveDialog.FileName:=FileName+'.xls';
                    Result:=SaveDialog.Execute;
                    UpdateWindow(GetActiveWindow);
                    if Result then
                      WorkBook.SaveAs(FileName:=SaveDialog.FileName);
                    SaveDialog.Free;
                end;
                Excel.Quit;
            end;
        Screen.Cursor:=OldCursor;
    end;