1。用delphi如何实现把sql server2k中的表记录导入新建的excel文档中
2。用delphi如何实现把sql server2k中的表记录附加导入已存在的excel文档中

解决方案 »

  1.   

    1搜一下以前的帖子
    2打开已导入数据的EXCEL文件,根据记录行数确定增加数据的起始行数,然后循环导入。
      

  2.   

    procedure TForm1.ExportData(DsTemp:TDataSource;DstTemp:TAdoDataSet);
    var
      ss : TStrings;
      i, j : integer;
      s, sFields, sTitle : string;
    begin
      if sdSave.Execute then
      begin
        sFields := ';';
        sTitle := '';
        For i := 0 to Self.ComponentCount - 1 do
          if (Self.Components[i] is TDBGrid) then
          begin
            if (Self.Components[i] as TDBGrid).DataSource = DsTemp then
            begin
              For j := 0 to (Self.Components[i] as TDBGrid).Columns.Count - 1 do
              begin
                sTitle := sTitle + (Self.Components[i] as TDBGrid).Columns[j].Title.Caption + #9;
                sFields := sFields + (Self.Components[i] as TDBGrid).Columns[j].FieldName + ';';
              end;
            end;
          end;
        j := DstTemp.FieldCount - 1;
        DstTemp.First;
        ss := TStringList.Create;
        ss.Add(sTitle);
        while not DstTemp.Eof do
        begin
          s := '';
          if sFields <> ';' then
          begin
            for i := 0 to j do
              if (Pos(';'+DstTemp.Fields[i].FieldName+';', sFields) > 0)
                or (DstTemp.Fields[i].DataType = ftMemo) then
                  s := s + DstTemp.Fields[i].asstring + #9;
          end
          else
          begin
            for i := 0 to j do
              s := s + DstTemp.Fields[i].asstring + #9;
          end;
          ss.Add(s);
          DstTemp.Next;
        end;
        ss.SaveToFile(sdSave.FileName);
        ss.free;
      end;
    end;
    sdsave.filename设为EXCEL文件。
      

  3.   

    Procedure TurnToExcel(TmpDBGrid:TDBGrid);
    var
      MyExcel: Variant;
      WorkBook: OleVariant;
      WorkSheet: OleVariant;
      i,j:integer;
      xlsfilename :string;
      Savedialog1 :TSaveDialog;
      fieldCount1 :Integer;
    begin
         FieldCount1 :=0;
         for j:=0 to TmpDBGrid.Columns.Count-1 do
           if TmpDBGrid.Columns[j].Visible=true then
           Inc(FieldCount1);
      with TmpDBGrid.DataSource.DataSet do
      if (Active) and (recordCount>0) then
      begin
      if Application.MessageBox(Pchar('共〖'+IntToStr(RecordCount)+' 行记录,'+IntToStr(FieldCount1)+' 列〗,确认导出到Excel?'),App_caption,MB_ICONQUESTION+MB_YESNO)=mrno then
         Abort;
      end
      else
        Abort;   
      SaveDialog1 :=TSaveDialog.create(Application);
      SaveDialog1.Filter := 'Excel文件(*.xls)|*.XLS';
      if savedialog1.Execute then
      if savedialog1.FileName <>'' then
      begin
        xlsfilename :=savedialog1.FileName;
      try
       MyExcel:=CreateOleObject('Excel.Application');
       MyExcel.Application.WorkBooks.Add;
       MyExcel.Caption:='将数据导入到EXCEL表中';
       MyExcel.Application.Visible:=false;
       WorkBook:=MyExcel.Application.workbooks[1];
       worksheet:=workbook.worksheets.item[1];
       except
         Application.MessageBox('EXCEL不存在!',App_caption,MB_ICONERROR+MB_OK);
        Savedialog1.Free;
        workBook.Saved := True;
        WorkBook.close;
        MyExcel.Quit;//释放VARIANT变量
        MyExcel:=Unassigned;
       end;
       i:=1;
       Frm_system_progress :=TFrm_system_progress.create(Application);
      Try
        with TmpDBGrid.DataSource.DataSet   do
        begin
          Open;
          DisableControls;
          with Frm_system_progress.ProgressBar_temp do
          begin
            min :=0;
            max :=TmpDBGrid.Columns.Count*recordcount;
            Position :=0;
          end;
          Frm_system_progress.label_progress.caption :='正在导出到Excel...';
          Frm_system_progress.Show;
          Frm_system_progress.update;
          for j:=0 to TmpDBGrid.Columns.Count-1 do
          begin
            if TmpDBGrid.Columns[j].Visible=true then
             worksheet.cells[1,j+1]:=TmpDBGrid.Columns[j].Title.Caption;
          end;
          First;
          while not Eof do
          begin
            inc(i);
            for j:=0 to TmpDBGrid.Columns.Count-1 do
            begin
              if TmpDBGrid.Columns[j].Visible=true then
              begin
                worksheet.cells[i,j+1].NumberFormatLocal :='@';
                worksheet.cells[i,j+1]:=TmpDBGrid.Columns[j].Field.AsString ;
                Frm_system_progress.ProgressBar_temp.StepIt;
              end;
            end;
            next;
          end;
          EnableControls;
        end;
        WorkBook.saveas(XlsFileName);
        Frm_system_progress.ProgressBar_temp.position :=TmpDBGrid.Columns.Count*TmpDBGrid.DataSource.DataSet.RecordCount;
        Application.MessageBox('导出到Excel成功!',App_caption,MB_ICONINFORMATION+MB_OK);
        Frm_system_progress.Free;
        MyExcel.Quit;
        MyExcel := Unassigned;
        Savedialog1.Free;
      except
        Application.MessageBox('导出到Excel失败!',App_caption,MB_ICONWARNING+MB_OK);
        workBook.Saved := True;
        WorkBook.close;
        MyExcel.Quit;//释放VARIANT变量
        MyExcel:=Unassigned;
        Frm_system_progress.Free;
        Savedialog1.Free;
      end;
      end;end;
      

  4.   

    用CreateOleObject,搜索一下以前帖子或在Google上找找,很多的!