我的程序需要动态建立一个EXCEL文件,HANK(星星农场)说直接保存一个就行了,
dengyf(cool)说要用TExcelWorkSheet,可我还是不会。那位大侠指导一下,详细一点。万分感谢!!!

解决方案 »

  1.   

    eclApp := CreateOleObject('Excel.Application');
      WorkBook :=CreateOleObject('Excel.Sheet');
      workBook:=eclApp.workBooks.Add;
      WorkBook.saveas('D:\a2.xls');
      WorkBook.Close;
      eclApp.Quit;
      eclApp:=Unassigned;
      

  2.   

    1.
    procedure TForm1.Button1Click(Sender: TObject);
    var
      mytxt : TStringList;
    begin
      mytxt := TStringList.Create;
      mytxt.SaveToFile('d:\新建XLS文件.xls');
      mytxt.Free;
    end;
    2.
    uses comobj;procedure TForm1.Button1Click(Sender: TObject);   
    var       xlsFilename :string;
              eclApp,WorkBook :variant ;
    begin
              xlsFileName :='新建EXCEL文件.xls';          try
                      eclApp :=CreateOleObject('Excel.Application');
                      WorkBook :=CreateOleObject('Excel.Sheet');
              except
                      showmessage('您的系统没有安装MS EXCEL');
                      exit;
              end;          try
                        WorkBook :=eclApp.workBooks.add ;
                    try
                         WorkBook.saveas(ExtractFilePath(Application.ExeName )+xlsFileName);
                         WorkBook.close;
                         showmessage('保存EXECL文件成功,路径为:'+ExtractFilePath(Application.ExeName )+xlsFileName);
                      except
                               showmessage('保存文件出错');
                      end;
                   except
                     showmessage('不能正确操作EXECL文件,可能该文件已经被其他程序占用或系统错误');
                     WorkBook.close;
                     eclApp.quit;
                     eclApp :=Unassigned;
             end;
    end;
      

  3.   

    unit dbtoexcel;interface
    uses oleserver, ConvUtils, ComObj,Dialogs,adodb,Classes,SysUtils,forms;
    type
      Tdbtoexcel =class
      private
      public
      function SaveDbToExcel(dataset : TADODataSet) : Byte;
    end;implementation{ Tdbtoexce }
    function Tdbtoexcel.SaveDbToExcel(dataset : TADODataSet): Byte;
    var
      ExcelApp,WorkBook : OleVariant;
      FieldName : string;
      FieldCount,i,j,RecordCount : Integer;
    begin
      try
        try
          ExcelApp := CreateOleObject('Excel.Application');
          WorkBook:=CreateOleObject('Excel.Sheet');
        except
          ShowMessage('没有安装EXCEL');
          Result := 0; //失败返回0
          exit;
        end;
        try
          ExcelApp.Caption := '应用程序调用 Microsoft Excel';
          WorkBook:=ExcelApp.workbooks.Add;
          dataset.Open;
          FieldCount := dataset.FieldCount;
          RecordCount := dataset.RecordCount;
          for i:=1 to FieldCount do //根据DATASET的字段名称写EXCEL的第一行.
          begin
            ExcelApp.Cells[1,i] :=dataset.Fields[i-1].FieldName;
          end;
          for i:=2 to RecordCount do //从第二行开始,把DATASET中的所有记录都写入EXCEL文件.
          begin
            for j:=1 to FieldCount do
            ExcelApp.Cells[i,j] :=dataset.FieldByName(dataset.Fields[j-1].FieldName).AsString;
            dataset.Next;
          end;
          WorkBook.SaveAs(ExtractFileDir(Application.ExeName)+'\test.xls');
          ShowMessage('文件已经保存为'+ExtractFileDir(Application.ExeName)+'\test.xls。您可以打开编辑此文件');
          Result := 1;//成功返回1
        except
          Result := 0; //失败返回0
        end;
      finally
        ExcelApp.WorkBooks.Close;
        ExcelApp.quit;
      end;
    end;end.
      

  4.   

    我来一个dbgrid内容转入excel文件
    Procedure DBGridToExcel(Grid:TDBGrid);
    var
      xlApp:Variant;//TexcelApplication;
      XlWorkBook:Variant;//_workBook;
      xlworkSheet:Variant;//_worksheet;
      I,J:integer;
       FileName:String;
    begin
        xlapp:=CreateOleObject('Excel.application');
        XlApp.Visible:=False;
        xlWorkBook:=xlApp.Workbooks.Add(-4167);
        xlWorkSheet:=xlapp.WorkBooks[1].Worksheets['sheet1'];// as _workSheet;
        Grid.DataSource.DataSet.DisableControls;
        I:=1;
        For J:=0 to Grid.FieldCount-1 do
        xlWorksheet.Cells[i,j+1]:=Grid.Columns[j].Title.Caption;
        I:=2 ;
           Grid.DataSource.DataSet.First;
           while not Grid.DataSource.DataSet.eof do
           begin
            For J:=0 to Grid.FieldCount-1 do
            xlworksheet.Cells[i,j+1]:=Grid.Fields[j].Value;
           i:=I+1;
           Grid.DataSource.DataSet.Next;
           end;//while
         Grid.DataSource.DataSet.EnableControls;
         xlWorkSheet.Range[xlWorkSheet.cells[1,1],xlWorkSheet.Cells[I,Grid.FieldCount]].columns.AutoFit;
         xlapp.visible:=True;end;
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      excelapp:variant;
      s:string;
    begin
      s:='myexcl';
     try
      excelapp:=CreateOleObject('excel.application');//创建EXCEL对象;
      excelapp.workbooks.add; //新建工作薄对象
      ExcelApp.WorkSheets[1].Activate;//工作薄获得焦点
      excelapp.ActiveWorkBook.SaveAs('C:\'+s+'.xls');//保存到文件
      excelapp.visible:=true;//设置EXCEL为可见
     finally
      EXIT;
     end;
    end;记得要在单元开头的uses中包含ComObj;
    可不要任何控件(当然要安装EXCEL)
      

  6.   

    如果是很简单的格式,直接保存成文本格式,把扩展名该为.xls,用电子表格打开的时候会自动转换成正确格式的。
      

  7.   

    不知其他人是怎么学的,我也是从网上找的资料,如超级猛料,Delphi技巧等,