Delphi深度历险(vcl.vclxx.org)中有一个控件可满足你的要求,具体叫什么名字我忘了,你可以去找一找。

解决方案 »

  1.   

    var v:olevariant;
    begin
    //首先建立引用的OLE对象,office97
      v:=CreateOleObject('Excel.Sheet.8');
      //通过V的APPLICATION接口访问EXCEL内部的方法和属性
      //如增加一工作簿
      v.application.workbooks.add('工作簿名');
      //其它的属性和方法你可以查阅EXCEL的帮助
    end;
      

  2.   

    hehe2000 or xp 
    how to deal with?
      

  3.   

    use activeX      //头文件中引用activex单元
        lcid:integer; //声明窗体级私有变量
    加入TExcelApplication,Texcelworkbook,Texcelworksheet控件function ChangeToExcel(....
    var i,j: Integer;
        MyArray:variant;
        M,N:INTEGER;
        FLD:STRING;
    begin
      lcid := GetUserDefaultLCID;
      eapp.Disconnect;
      eapp.Connect;
      eapp.Visible[0]:=True;
      eWB.ConnectTo(eapp.Workbooks.Add(TOleEnum(xlWBATWorksheet), lcid));
      eWS.ConnectTo(ewb.Worksheets[1] as _Worksheet);
      q_result.first;
      q_result.DisableControls;
      IF q_result.FieldCount>26 THEN
      BEGIN
        M:= q_result.FieldCount DIV 26;
        N:= q_result.FieldCount MOD 26;
        IF N>0 THEN FLD:= chr(M+64)+chr(N+64)
        ELSE FLD:= chr(M-1+64)+'Z';
        END
      ELSE FLD:=chr(q_result.FieldCount+64);
      MyArray:=VarArrayCreate([0,(q_result.FieldCount-1)],varVariant);
      for i:=0 to q_result.Fieldcount-1 do
      begin
        MyArray[i]:= DBgrideh2.Columns[i].Title.Caption ;
      end;
      eWS.Range['A1', (FLD+'1')].Value := MyArray;
      for i:=2 to q_result.RecordCount+1 do
      begin
        for j:=0 to q_result.FieldCount-1 do
        begin
          MyArray[j]:= q_result.Fields[j].Value ;
        end;
        eWS.Range['A'+intTostr(i), (FLD+intTostr(i))].Value := MyArray;
        q_result.Next;
      end;
      q_result.EnableControls;
    end;
    //这段代码是以显式的方法把数据转入Excel,需要隐式转入,只需设Texcelapplication.visible:=false;
    至于保存成文件,调用Texcelworksheet.saveas()方法即可 
      

  4.   

    这是我的代码procedure TForm1.Button1Click(Sender: TObject);
    var
      i,j,h,k:Integer;
      handle : HWND;
    begin
      {Handle := FindWindow('XLMAIN', nil);    //判断Excel是否执行
      if handle <> 0 then
        showmessage('Excel is Running!')
      else
        showmessage('not Excel is Running!');
      }
      ExcelExistFlag := True;
      try
        EApp := CreateOleObject('Excel.Application.9') as _Application;  //Excel2000
      except
        ExcelExistFlag := False;
      end;
      if not ExcelExistFlag then
      try
        EApp := CreateOleObject('Excel.Application.8') as _Application;  //Excel97
        ExcelExistFlag := True;
      except
        ExcelExistFlag := False;
        ShowMessage('Excel调用失败!');
      end;  //EApp := CreateOleobject('Excel.Application');  if ExcelExistFlag then
      begin
        ADOQuery1.DisableControls;
        EApp.Visible:=true;
        //EApp.Caption := '还不行??';
        //EApp.Workbooks.Add;
        EApp.Workbooks.Add(xlWBatWorkSheet);
        EApp.Workbooks[1].WorkSheets[1].Name := Label1.Caption;
        ESheet := EApp.Workbooks[1].WorkSheets[Label1.Caption];
        ExcelCellRange := EApp.WorkBooks[1].WorkSheets[Label1.Caption].Columns;
        ADOQuery1.First;    for j:=0 to ADOQuery1.FieldCount-1  do
        begin
          //ESheet.Cells[1,j+1] :='';
          //ESheet.Cells[2,j+1] :='';
          //ESheet.Cells[3,j+1] :='';
          h:=Trunc(ADOQuery1.FieldCount/2);   //取整
          ESheet.Cells[1,h] :='北京tikkypeng科技开发有限公司';
          ESheet.Cells[2,h] :='用户信息表';
          ESheet.Cells[3,h] :='';
          ESheet.Cells[4,j+1] := DBGrid1.Columns[j].Title.Caption;
          //EApp.WorkBooks[1].WorkSheets[1].Cells[4,j+1].ColumnWidth := DBGrid1.Columns[j].Width;
          EApp.WorkBooks[1].WorkSheets[1].Cells[4,j+1].Borders.LineStyle := xlContinuous; //边框类型
          EApp.WorkBooks[1].WorkSheets[1].Cells[4,j+1].Borders.Weight := xlMedium;        //边框粗细
          //ExcelCellRange.Columns[j+1].ColumnWidth :=DBGrid1.Columns[j].Field.Size;// DisplayWidth  ;
          ExcelCellRange.Columns[j+1].ColumnWidth := DBGrid1.Columns[j].Width/7;
          //ExcelCellRange.Columns.Item[j+1].Name:=DBGrid1.Columns[j].Title.Caption ;
          //ExcelCellRange.Characters.Font.Name := '宋体'; //字体
          //ExcelCellRange.Characters.Font.FontStyle := '加粗';
          //ExcelCellRange.Characters.Font.Size := 9;
          //ExcelCellRange.Characters.Font.Color := clBlue; //颜色
          //ExcelCellRange.Characters.Font.ColorIndex := xlAutomatic; //颜色
          //ExcelCellRange := EApp.WorkBooks[1].WorkSheets[1].Range['A1:D4'];
          //ExcelCellRange.Borders.LineStyle := xlDouble;
        end;
        for i:=1 to ADOQuery1.RecordCount  do
        begin
          for j:=1 to ADOQuery1.FieldCount  do
          begin
            ESheet.Cells[i+4,j] :=ADOQuery1.Fields[j-1].AsString;
            EApp.WorkBooks[1].WorkSheets[1].Cells[i+4,j].Borders.LineStyle := xlContinuous; //边框类型
            EApp.WorkBooks[1].WorkSheets[1].Cells[i+4,j].Borders.Weight := xlThin;          //边框粗细
            //ESheet.Cells[i+2,j].Characters.Font.Name := '宋体'; //字体
            //ESheet.Cells[i+2,j].Characters.Font.FontStyle := '';
            //ESheet.Cells[i+2,j].Characters.Font.Size := 9;      //大小
            //ESheet.Cells[i+2,j].Characters.Font.Color := clRed; //颜色
            //ESheet.Cells[i+1,j].Characters.Font.ColorIndex := xlAutomatic; //颜色
          end;
          ADOQuery1.next;
        end;
        k:=ADOQuery1.RecordCount+8;
        ESheet.Cells[k,1] :='★Vigorous★';
        ESheet.Cells[k,h] :=DateToStr(Date);   //'2001-03-14';
        ESheet.Cells[k,ADOQuery1.FieldCount] :='☆★☆★☆★☆'; //'tikkypeng打印';
        //HandleRange;
        ADOQuery1.EnableControls;
        //EApp.Visible:=True;
      end
      else
        ShowMessage('调用Excel2000或Excel97失败,请确认是否安装!'+#13#13+'如果未安装,请先安装office');
        //ExcelCellRange.Characters.Font.Name := '宋体'; //字体
        //ExcelCellRange.Characters.Font.FontStyle := '';
        //ExcelCellRange.Characters.Font.Size := 9;
        {EApp.WorkBooks[1].WorkSheets[1].Range['A1'].Borders[xlBottom].LineStyle := xlContinuous;
        EApp.WorkBooks[1].WorkSheets[1].Range['A1'].Borders[xlBottom].Weight := xlHairline;
        EApp.WorkBooks[1].WorkSheets[1].Range['A2'].Borders[xlBottom].LineStyle := xlDash;  //虚线
        EApp.WorkBooks[1].WorkSheets[1].Range['A2'].Borders[xlBottom].Weight := xlThin;  //虚线
        EApp.WorkBooks[1].WorkSheets[1].Range['A3'].Borders.LineStyle := xlContinuous;
        EApp.WorkBooks[1].WorkSheets[1].Range['A3'].Borders.Weight := xlMedium;
        EApp.WorkBooks[1].WorkSheets[1].Range['A4'].Borders[xlBottom].LineStyle := xlDashDot;
        EApp.WorkBooks[1].WorkSheets[1].Range['A4'].Borders[xlBottom].Weight := xlThick;
        }
    end;
      

  5.   

    ExcelApplication1.Rows.CopyFromRecordset(ADOQuery1.RecordSet.Get_DataSource, 10000, 100);这样速度更快