我编一个打开EXCEL文件的程序,这个程序运行时可以正常打开EXCEL文件,但当选择“取消”(既不打开文件而直接关闭选择框)的时候却会给我报个错,
而且还提示我“请选择EXCEL文件”。请问这个问题怎么解决呢??附上我写的:
procedure TForm2.Button3Click(Sender: TObject);
Var
i,j:integer;
begin
 opendialog1.InitialDir:=ExtractFileDir(paramstr(0));
  opendialog1.Execute;
   Try
   ExcelApplication1.Connect;
   Except
   MessageDlg('请先安装Excel',mtError,[mbOk],0);
   Abort;
   End;
  ExcelApplication1.Visible[0]:=True;
  ExcelApplication1.Caption:='Excel应用';
  try
  excelapplication1.Workbooks.Open(OpenDialog1.FileName,
  null,null,null,null,null,null,null,null,null,null,null,null,0);
  except
 begin
 ExcelApplication1.Disconnect;
 ExcelApplication1.Quit;
 showmessage('请选择EXCEL文件');
 exit;
 end;
 end;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
stringgrid1.Cells[1,0]:='Wavelength';
stringgrid1.Cells[2,0]:='AD';
for i:=6 to 3011 do
for j:=1 to 3 do
begin
if trim(ExcelWorksheet1.Cells.Item[i+1,1])<>''then
begin
stringgrid1.rowCount:=i+1;
stringgrid1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i+1,j];
end
else
begin
label3.caption:=inttostr(i-1);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Edit1.Text:=stringgrid1.Cells[1,1];
Edit2.Text:=stringgrid1.Cells[2,1];
Edit3.Text:=stringgrid1.Cells[3,1];
Exit;
end;
end;
end;

解决方案 »

  1.   

    procedure TForm2.Button3Click(Sender: TObject);
    Var
    i,j:integer;
    begin
     opendialog1.InitialDir:=ExtractFileDir(paramstr(0));
      //opendialog1.Execute;
      if opendialog1.Execute then
       Try
       ExcelApplication1.Connect;
       Except
       MessageDlg('请先安装Excel',mtError,[mbOk],0);
       Abort;
       End;
      ExcelApplication1.Visible[0]:=True;
      ExcelApplication1.Caption:='Excel应用';
      try
      excelapplication1.Workbooks.Open(OpenDialog1.FileName,
      null,null,null,null,null,null,null,null,null,null,null,null,0);
      except
     begin
     ExcelApplication1.Disconnect;
     ExcelApplication1.Quit;
     showmessage('请选择EXCEL文件');
     exit;
     end;
     end;
    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
    stringgrid1.Cells[1,0]:='Wavelength';
    stringgrid1.Cells[2,0]:='AD';
    for i:=6 to 3011 do
    for j:=1 to 3 do
    begin
    if trim(ExcelWorksheet1.Cells.Item[i+1,1])<>''then
    begin
    stringgrid1.rowCount:=i+1;
    stringgrid1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i+1,j];
    end
    else
    begin
    label3.caption:=inttostr(i-1);
    ExcelApplication1.Disconnect;
    ExcelApplication1.Quit;
    Edit1.Text:=stringgrid1.Cells[1,1];
    Edit2.Text:=stringgrid1.Cells[2,1];
    Edit3.Text:=stringgrid1.Cells[3,1];
    Exit;
    end;
    end;
    end;
    ==================================
    以下内容纯属广告,不可能雷同
    e族百变桌面--特效更换桌面壁纸,丰富的网上资源支持,
    Flash桌面,视频桌面……让您的桌面动起来!
    http://www.popolong.com
    ==================================
      

  2.   

    procedure TForm2.Button3Click(Sender: TObject);
    Var
    i,j:integer;
    begin
     opendialog1.InitialDir:=ExtractFileDir(paramstr(0));
      //opendialog1.Execute;
      if opendialog1.Execute then
      begin
       Try
       ExcelApplication1.Connect;
       Except
       MessageDlg('请先安装Excel',mtError,[mbOk],0);
       Abort;
       End;
      ExcelApplication1.Visible[0]:=True;
      ExcelApplication1.Caption:='Excel应用';
      try
      excelapplication1.Workbooks.Open(OpenDialog1.FileName,
      null,null,null,null,null,null,null,null,null,null,null,null,0);
      except
     begin
     ExcelApplication1.Disconnect;
     ExcelApplication1.Quit;
     showmessage('请选择EXCEL文件');
     exit;
     end;
     end;
    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
    stringgrid1.Cells[1,0]:='Wavelength';
    stringgrid1.Cells[2,0]:='AD';
    for i:=6 to 3011 do
    for j:=1 to 3 do
    begin
    if trim(ExcelWorksheet1.Cells.Item[i+1,1])<>''then
    begin
    stringgrid1.rowCount:=i+1;
    stringgrid1.Cells[j,i]:=ExcelWorksheet1.Cells.Item[i+1,j];
    end
    else
    begin
    label3.caption:=inttostr(i-1);
    ExcelApplication1.Disconnect;
    ExcelApplication1.Quit;
    Edit1.Text:=stringgrid1.Cells[1,1];
    Edit2.Text:=stringgrid1.Cells[2,1];
    Edit3.Text:=stringgrid1.Cells[3,1];
    Exit;
    end;
    end;
    end;
    end;==================================
    以下内容纯属广告,不可能雷同
    e族百变桌面--特效更换桌面壁纸,丰富的网上资源支持,
    Flash桌面,视频桌面……让您的桌面动起来!
    http://www.popolong.com
    ==================================
      

  3.   

    //opendialog1.Execute;
      if opendialog1.Execute then
      begin
       Try
    ......
    Exit;
    end;
    end;
    end;
    end;小伙,看清楚了,第一遍我少写个begin ...  end==================================
    以下内容纯属广告,不可能雷同
    e族百变桌面--特效更换桌面壁纸,丰富的网上资源支持,
    Flash桌面,视频桌面……让您的桌面动起来!
    http://www.popolong.com
    ==================================