我想寫一條語句,把查詢結果插入到excel中,請問怎麼寫?
順便問一下按結果集是中文,想要排序,應該怎樣寫?高分求助!

解决方案 »

  1.   

    添加控件excelapplication,excelworkbooks,excelworksheet
    qy_**是adoquery控件
    你把查询的结果跟我写的程序做一个修改就可以了!
    procedure Tfrm_xgs.Button3Click(Sender: TObject);
    var
      i,j,row,column:integer;
    begin
      try
         excelapplication1.Connect ;
      except
          messagedlg('不能打开excel表格,请检查',mterror,[mbok],0);
          abort;
      end;
        try
         excelapplication1.Visible[0]:=true;
         excelapplication1.Workbooks.Add(null,0);
         excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
         excelworksheet1.Connectto(excelapplication1.Worksheets[1] as _worksheet);
      if qy_xl_de.Active then
         begin      row:=1;
         while not(qy_xl_de.eof) do
         begin
            j:=1;
           column:=1;
           for i:=0 to qy_xl_de.FieldCount-1 do
              begin
                excelworksheet1.Cells.Item[1,j]:=dbgrid1.Columns[i].Title.Caption ;
                excelworksheet1.Cells.Item[row+1,column]:=qy_xl_de.Fields[i].AsString ;
                j:=j+1;
                column:=column+1;          end;
              qy_xl_de.Next ;
              row:=row+1;
          end ;
          qy_xl_de.Active :=false;
          end
      else if qy_bd_de.Active then
         begin      row:=1;
          while not(qy_bd_de.eof) do
        begin
           j:=1;
           column:=1;
           for i:=0 to qy_bd_de.FieldCount-1 do
              begin
                excelworksheet1.Cells.Item[1,j]:=dbgrid1.Columns[i].Title.Caption ;
                excelworksheet1.Cells.Item[row+1,column]:=qy_bd_de.Fields[i].AsString ;
                 j:=j+1;
                 column:=column+1;
              end;   
              qy_bd_de.Next ;
              row:=row+1;
          end;
          qy_bd_de.Active :=false;
          end
      else if qy_tj_de.Active then
         begin      row:=1;
        while not(qy_tj_de.eof) do
        begin
            j:=1;
           column:=1;
           for i:=0 to qy_tj_de.FieldCount-1 do
              begin
                excelworksheet1.Cells.Item[1,j]:=dbgrid1.Columns[i].Title.Caption ;
                excelworksheet1.Cells.Item[row+1,column]:=qy_tj_de.Fields[i].AsString ;
                  j:=j+1;
                  column:=column+1;
              end;
              qy_tj_de.Next ;
              row:=row+1;
          end;
           qy_tj_de.Active :=false;
          end;
      except
       abort;
       end;
      end;
      

  2.   

    http://expert.csdn.net/Expert/topic/1467/1467372.xml?temp=.5728723
      

  3.   

    用DXDBGRID控件,是个好注意,
    51delphi有下