Delphi6 + Excel 怎么做呢?
如何把一查询到的记录导入Excel呢?
帮帮我好吗?Up也有分的;
有例子吗?没有也可以的,只要能说一说;

解决方案 »

  1.   

    DELPHI操作EXCEL的文章很多,你可以搜索啊;
      

  2.   

    此例是将查询的结果转为*.CSV文当,然后直接用EXCEL打开!procedure Tfrmtoexcel.DBLookupComboBox1Click(Sender: TObject);
    begin
    dm.tbtoexcel.close;
    dm.tbtoexcel.sql.clear;
    dm.tbtoexcel.sql.add('select * from  '+trim(DBLookupComboBox1.Text));
    dm.tbtoexcel.open;
    dm.tbtoexcel.active:=true;
    dbgrid1.DataSource:=dm.dstoexcel;
    dbgrid1.DataSource.DataSet:=dm.tbtoexcel; 
    end;procedure Tfrmtoexcel.BitBtn1Click(Sender: TObject);
    var
      i,j:integer;
      f:TextFile;
      sl:TStringList;
    begin
    with dm.tbtoexcel do
    begin
      if (not dm.tbtoexcel.Active) then
          Exit;    if (not SaveDialog1.Execute) then
          Exit;    AssignFile(f,SaveDialog1.FileName);
        try
          Rewrite(f);
        except
          Showmessage('保存文件失敗!');
          CloseFile(f);
          Exit;
        end;    Screen.Cursor := crHourGlass;
        DisableControls;
        sl := TStringList.Create;
        sl.Clear;
        for i:=0 to FieldCount-1 do
        begin
          if (Fields[i].Visible) then
            sl.Add(Fields[i].DisplayLabel);
        end;
        writeln(f,sl.CommaText);    First;
        for j:=0 to RecordCount-1 do
        begin
          sl.Clear;
          for i:=0 to FieldCount-1 do
          begin
            if (Fields[i].Visible) then
              sl.Add(VarToStr(Fields[i].Value));
          end;
          writeln(f,sl.CommaText);
          Next;
        end;    sl.Free;
        CloseFile(f);
        EnableControls;
        Screen.Cursor := crDefault;
    end;    // withend;
      

  3.   

    TO 流星:
    可是delphi6的我没有找到啊;
    5的在6的上边好像不可以用的;
      

  4.   

    是小师妹呀
    好吧,我将我以前作的一个单元给你,把分给我吧
    如果有不明白的地方,上Msn或QQ
      

  5.   

    TO sonicer(极品瘦子猪哥靓) 
    你是狐狸,对不对;
    呵呵,分不能全给你的,要给这个贴子的,另外给你开个贴子;
      

  6.   

    TO  xxyzjf(FaceToFace) 
    我希望直接导入到Excel里;
      

  7.   

    TO xxyzjf(FaceToFace) 
    我希望可以直接导入到Excel里
      

  8.   

    这个问题有很多讨论,自己去搜索一下好了,给你个url
    http://www.csdn.net/expert/topic/996/996596.xml?temp=.2106592
      

  9.   

    *.csv文件可以直接用EXCEL打开的!
    考虑到速度的问题所以用CSV.
      

  10.   

    var
      i, Row, Column: Integer;
      RecordCounts,FieldsCount: Integer;
      ExcelSheet,Range: OleVariant;
      XLApp : OleVariant;
    begin
      try
        XLApp:= CreateOLEObject('Excel.Application');
        XLApp.Visible:= True;
      except
        ShowMessage('请检查有没有安装Excel?');
        Abort;
      end;
      XLApp.Application.Caption:= '数据表';
      XLApp.workBooks.Add(XlWBatWorkSHeet);
      XLApp.workBooks[1].workSHeets[1].Name:='Xgto';
      Row:= 1;
      with Query1 do
      begin
        Close;
        UnPrePare;
        SQL.Clear;
        SQL.Add('Select * from zcbdb');
        PrePare;
        Open;
        First;
        Last;
        RecordCounts := RecordCounts;
        FIeldsCount := FieldCount;
      end;
      if RecordCounts < 1 then
      begin
        ShowMessage('aaaastrg');
        Exit;
      end;
      //设置列宽
        XLApp.Range['A1', 'A1'].ColumnWidth := 10;
        XLApp.Range['B1', 'B1'].ColumnWidth := 20;
        XLApp.Range['C1', 'C1'].ColumnWidth := 40;
        XLApp.Range['D1', 'D1'].ColumnWidth := 10;
        XLApp.Range['E1', 'E1'].ColumnWidth := 10;
        XLApp.Range['F1', 'F1'].ColumnWidth := 10;
        XLApp.Range['G1', 'G1'].ColumnWidth := 10;  //设置列头
        ExcelSheet:= XLApp.WorkBooks[1].WorkSheets['明细列表'];
      //设置报表横向显示
        ExcelSheet.PageSetup.Orientation := xlLandscape;
      //设置第一列为字符方式
        XLApp.Range['A:A'].NumberFormatLocal:= '@';
        ExcelSheet.Cells.Item[Row,1]:= '字段名1';
        ExcelSheet.Cells.Item[Row,2]:= '字段名1';
        ExcelSheet.Cells.Item[Row,3]:= '字段名1';
        ExcelSheet.Cells.Item[Row,4]:= '字段名1';
        ExcelSheet.Cells.Item[Row,5]:= '字段名1';
        ExcelSheet.Cells.Item[Row,6]:= '字段名…';
        ExcelSheet.Cells.Item[Row,7]:= '字段名n';
        Row:= Row+1;
        with Query1 do
        begin
          Close;
          Open;
          while Not Eof do
          begin
            Column:= 1;
            for i:=1 to FieldsCount do
            begin
              Excelsheet.Cells.Item[Row,Column]:= Fields[i-1].AsString;
              Column:= Column+1;
            end;
            Row:= Row+1;
            Next;
          end;
          Excelsheet.Columns.AutoFit;
          //画单元格边框
        end;
    end;
      

  11.   

    参照下面程序做一个过程我是这样做的(过程中有一个提示form(wiat_tsForm) 可以删掉或新建):
    PROCEDURE PUB_INSET_EXCEL(DataSet: TDataSet;dbgrid1 :Tdbgrid);
    var
      i,j: Integer;
      Sheet,ColumnRange: Variant;begin
      try
      wiat_tsForm :=Twiat_tsForm.create(application);
      wiat_tsForm.show;
      wiat_tsForm.UPDATE;wiat_tsForm.Label7.caption :='正在处理数据,共有'+inttostr(dataset.RecordCount)+'用户数据,请稍后...';
    wiat_tsForm.ProgressBar1.Visible :=true;
    wiat_tsForm.Label6.Visible :=true;
    wiat_tsForm.Animate1.Visible :=false;
    wiat_tsForm.Animate2.Visible :=true;
    wiat_tsForm.Animate2.BringToFront ;wiat_tsForm.update;
    XLApp:= CreateOleObject('Excel.Application');
      XLApp.Workbooks.Add(xlWBatWorkSheet);
      XLApp.Workbooks[1].WorkSheets[1].Name := 'jlbtj';    Sheet := XLApp.Workbooks[1].WorkSheets['jlbtj'];
       ColumnRange := XLApp.Workbooks[1].WorkSheets['jlbtj'].Columns;
       dataset.First ;
           for i :=1 to dbgrid1.Columns.Count do
              Sheet.Cells[1, i] :=dbgrid1.Columns[i-1].Title.Caption ;         wiat_tsForm.ProgressBar1.Min :=0;
             wiat_tsForm.ProgressBar1.Max :=dataset.RecordCount+1;
             wiat_tsForm.ProgressBar1.Step := 1;  for i := 2 to dataset.RecordCount+1  do
        begin
            wiat_tsForm.update;
            wiat_tsForm.label6.caption :=inttostr(round(i*100/dataset.RecordCount))+'%';
            wiat_tsForm.ProgressBar1.StepIt ;
          for j:=1 to dbgrid1.Columns.Count do
             begin
               Sheet.Cells[i, j] := DBGrid1.Fields[j-1].asstring;
             end;
         dataset.Next ;
        end;
      finally
        wiat_tsForm.close;wiat_tsForm.Free;
          XLApp.Visible := True;
      end;
    END;