var
  ExcelApplication1: TExcelApplication;
  ExcelWorksheet1: TExcelWorksheet;
  ExcelWorkbook1: TExcelWorkbook;
begin
  begin
    Try
        ExcelApplication1.Connect;
        Except
          MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
          Abort;
        End;
        ExcelApplication1.Visible[0]:=True;
        ExcelApplication1.Caption:='天平数据记录';
        ExcelApplication1.Workbooks.Add(Null,0);
        ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
        ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
        ExcelWorksheet1.Cells.Item[1,1].value := '实验日期';
        ExcelWorksheet1.Cells.Item[1,2].value := '班次';
        ExcelWorksheet1.Cells.Item[1,3].value := '操作员';
        ExcelWorksheet1.Cells.Item[1,4].value := '班长';
        ExcelWorksheet1.Cells.Item[1,5].value := '起始时间';
        ExcelWorksheet1.Cells.Item[1,6].value := '结束时间';
        ExcelWorksheet1.Cells.Item[1,7].value := '累计用料'; 
        
        
        ExcelWorksheet1.Columns.AutoFit;                
        ExcelWorksheet1.Disconnect;        
        ExcelWorkbook1.Disconnect;
        ExcelApplication1.Disconnect;
        ExcelWorksheet1.free;       
        ExcelWorkbook1.free;
        ExcelApplication1.free;
        ExcelApplication1.ActiveWorkbook.SaveAs('C:\1.xls',null,null,null,null,null,null,null,null,null,null,0);//这里出错!        
  end;   
end;   
请教高手,为什么保存总是出错!
我希望的结果是:数据自动输出保存到我指定的文件夹中!

解决方案 »

  1.   

    var
     iRow,iBeginRow:integer;
             i,j:integer;
       SaveFileName:String;
    begin
      SaveFileName:='D:\dd.xls';
      if  SaveFileName='' then
    exit;
    try
    try
    xlApp.ConnectKind:=ckNewInstance;
    xlApp.Connect;
    xlApp.Visible[0]:=True;
    except
    Application.MessageBox('没有安装Excel2000.','程序提示',mb_iconinformation);
    exit;
    end;
    xlApp.WorkBooks.Add(EmptyParam,0);
    xlBook.ConnectTo(xlApp.Workbooks[1]);
    xlSheet.ConnectTo(xlBook.Worksheets[1] as _WorkSheet);
    //打印和页面设置
    xlSheet.PageSetup.Orientation:=xlLandscape;
    xlSheet.PageSetup.LeftMargin := xlApp.InchesToPoints(0.354330708661417);
    xlSheet.PageSetup.RightMargin := xlApp.InchesToPoints(0.354330708661417);
    xlSheet.PageSetup.TopMargin := xlApp.InchesToPoints(0.511811023622047);
    xlSheet.PageSetup.BottomMargin := xlApp.InchesToPoints(0.511811023622047);
    xlSheet.PageSetup.HeaderMargin := xlApp.InchesToPoints(0.511811023622047);
    xlSheet.PageSetup.FooterMargin := xlApp.InchesToPoints(0.275590551181102);
    xlSheet.PageSetup.PrintTitleRows := '$1:$2';
    //页眉页脚
    // xlSheet.PageSetup.LeftFooter:= '日期:&D';
    // xlSheet.PageSetup.RightFooter:='第 &P 页,共 &N 页'; //列宽
                    for i := 1 to 13 do
                    begin
                      xlSheet.Cells.Item[1,i].ColumnWidth:=8;
                    end; //标题
    xlSheet.Cells.Item[1,6]:=format('%d年【%s】逐日平均水位表',[2004, 'Name']);
    xlSheet.Cells.Item[1,6].Font.Size:=18;
    xlSheet.Cells.Item[1,6].Font.name:='黑体';
    xlSheet.Cells.Item[1,6].Font.Bold:=True;
    xlSheet.Range[xlSheet.Cells.Item[1,1],xlSheet.Cells.Item[1,13]].Merge(EmptyParam);
    xlSheet.Cells.Item[1,1].HorizontalAlignment:=xlCenter;
    xlSheet.Cells.Item[1,1].VerticalAlignment:=xlTop;
    xlSheet.Cells.Item[1,1].RowHeight:=27;
                    //副标题
    xlSheet.Cells.Item[2,6]:='表内水位(冻结基面以上米数)'+ '米='+'基面以上米数';
    xlSheet.Cells.Item[2,6].Font.Size:=9;
    xlSheet.Cells.Item[2,6].Font.name:='黑体';
    xlSheet.Range[xlSheet.Cells.Item[2,1],xlSheet.Cells.Item[2,13]].Merge(EmptyParam);
    xlSheet.Cells.Item[2,1].HorizontalAlignment:=xlRight; //表头
    iRow:=3;
    xlSheet.Cells.Item[iRow,1]:='日\月';
    xlSheet.Cells.Item[iRow,2]:='一月';
    xlSheet.Cells.Item[iRow,3]:='二月';
    xlSheet.Cells.Item[iRow,4]:='三月';
    xlSheet.Cells.Item[iRow,5]:='四月';
    xlSheet.Cells.Item[iRow,6]:='五月';
    xlSheet.Cells.Item[iRow,7]:='六月';
    xlSheet.Cells.Item[iRow,8]:='七月';
    xlSheet.Cells.Item[iRow,9]:='八月';
    xlSheet.Cells.Item[iRow,10]:='九月';
    xlSheet.Cells.Item[iRow,11]:='十月';
    xlSheet.Cells.Item[iRow,12]:='十一月';
    xlSheet.Cells.Item[iRow,13]:='十二月';
    xlSheet.Range[xlSheet.Cells.Item[iRow,1],xlSheet.Cells.Item[iRow,13]].Font.Bold:=True;
    xlSheet.Range[xlSheet.Cells.Item[iRow,1],xlSheet.Cells.Item[iRow,13]].HorizontalAlignment:= xlCenter;
                    iBeginRow:=iRow;
                      for j := 1 to 31  do
                      begin
                          xlSheet.Cells.Item[j+iRow,1]:=inttostr(j);
                          if (j mod 10 =0) and (j<>30) then
                          begin
                            iRow:=iRow+1;
                            xlSheet.Range[xlSheet.Cells.Item[j+iRow,1],xlSheet.Cells.Item[j+iRow,13]].Merge(EmptyParam);
                          end;
                      end;                //   填写页脚内容
                    iRow:=iRow+32;
                    iBeginRow:=iRow;
             xlSheet.Cells.Item[iRow,1]:='平均水位';
                    for i :=1 to 12 do
                    begin
                           xlSheet.Cells.Item[iRow,i+1]:='33';
                    end;
                    iRow:=iRow+1;
             xlSheet.Cells.Item[iRow,1]:='最高水位';
                    for i :=1 to 12 do
                    begin
                           xlSheet.Cells.Item[iRow,i+1]:='434';
                    end;
                    iRow:=iRow+1;
             xlSheet.Cells.Item[iRow,1]:='最高日期';
                    for i :=1 to 12 do
                    begin
                           xlSheet.Cells.Item[iRow,i+1]:='4234';
                    end;
                    iRow:=iRow+1;
             xlSheet.Cells.Item[iRow,1]:='最低水位';
                    for i :=1 to 12 do
                    begin
                           xlSheet.Cells.Item[iRow,i+1]:='455';
                    end;
                    iRow:=iRow+1;
             xlSheet.Cells.Item[iRow,1]:='最低日期';
                    for i :=1 to 12 do
                    begin
                           xlSheet.Cells.Item[iRow,i+1]:='4334';
                    end;
                    iRow:=iRow+1;
             xlSheet.Cells.Item[iRow,1]:='年统计';
                    xlSheet.Range[xlSheet.Cells.Item[iRow,10],xlSheet.Cells.Item[iRow,13]].Merge(EmptyParam);
                    xlSheet.Cells.Item[iRow,3].HorizontalAlignment:=xlCenter;
                    iRow:=iRow+1;
             xlSheet.Cells.Item[iRow,1]:='附注';
                    xlSheet.Range[xlSheet.Cells.Item[iRow,1],xlSheet.Cells.Item[iRow,13]].Merge(EmptyParam);
                    iRow:=iRow+1; xlSheet.Range[xlSheet.Cells.Item[3,1],xlSheet.Cells.Item[iRow-1,13]].Borders.LineStyle := xlContinuous;
        xlSheet.SaveAs(SaveFileName);
    finally
        xlApp.Disconnect;
        xlBook.Close;
        xlApp.Disconnect;
        xlApp.Quit;
        xlSheet.Disconnect;
        xlBook.Disconnect;
    end;
        showmessage('保存数据文件:['+SaveFileName+ ']成功!')
      

  2.   

    有一点不理解,为什么我存
    ExcelWorkbook1.saveas(Savefilename);
    的时候就出错;
    而存
    ExcelWorksheet1.saveas(Savefilename);
    的时候就可以呢;
    按道理说应该是存ExcelWorkbook1才对啊,再说ExcelWorkbook1也支持SaveAs方法;
      

  3.   

    还有谁人能告知
    ExcelWorkbook1.Disconnect;
    ExcelWorkbook1.close;
    ExcelWorkbook1.free;
    三者的意义和区别关闭EXCEL的时候怎么释放
    ExcelApplication1 
    ExcelWorkbook1 
    ExcelWorksheet1
    以及三者的操作顺序;