D
dedragon
Unregistered / Unconfirmed
GUEST, unregistred user!
我想用一个按钮搜索出excel的2个worksheet的内容,然后显示在2个memo里,代码如下:procedure TForm1.Button5Click(Sender: TObject);var i,j,k,l:integer;begin opendialog1.InitialDir:=ExtractFileDir(paramstr(0));//文件的打存放初始路径 //opendialog1.Execute; Try ExcelApplication1.Connect;//EXCEL应用程序 Except MessageDlg('Excel may not be installed',mtError, [mbOk], 0); Abort; End; ExcelApplication1.Visible[0]:=True; ExcelApplication1.Caption:='Excel Application'; try //excelapplication1.Workbooks.Open(opendialog1.FileName,null,null,null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件 excelapplication1.Workbooks.Open('F:/ExcelTest/1.xls',null,null,null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件 except begin ExcelApplication1.Disconnect;//出现异常情况时关闭 ExcelApplication1.Quit; showmessage('请选择EXCEL电子表格!'); exit; end; end; ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);//ExcelWorkbook1与Eexcelapplication1建立连接 ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1与Excelworkbook1建立连接 ExcelWorkbook2.ConnectTo(ExcelApplication1.Workbooks[1]);//ExcelWorkbook2与Eexcelapplication1建立连接 ExcelWorksheet2.ConnectTo(ExcelWorkbook2.Worksheets[2] as _Worksheet);//Excelworksheet2与Excelworkbook2建立连接//开始从EXCEL中取数,放到stringgrid1中,取完数后关闭EXCEL for i:=1 to 1000 do//最大取值1000 for j:=1 to 6 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; //将第一条数据赋给编辑框 memo1.Lines[0]:=stringgrid1.Cells[1,1]; memo1.Lines[1]:=stringgrid1.Cells[2,1]; memo1.Lines[2]:=stringgrid1.Cells[3,1]; memo1.Lines[3]:=stringgrid1.Cells[4,1]; memo1.Lines[4]:=stringgrid1.Cells[5,1]; memo1.Lines[5]:='*************'; memo2.Lines[0]:=stringgrid2.Cells[1,1]; memo2.Lines[1]:=stringgrid2.Cells[2,1]; memo2.Lines[2]:=stringgrid2.Cells[3,1]; memo2.Lines[3]:=stringgrid2.Cells[4,1]; memo2.Lines[4]:=stringgrid2.Cells[5,1]; memo2.Lines[5]:='*************'; // edit1.text:=stringgrid1.Cells[1,1]; // edit2.text:=stringgrid1.Cells[2,1]; // edit3.text:=stringgrid1.Cells[3,1]; // edit4.text:=stringgrid1.Cells[4,1]; // edit5.text:=stringgrid1.Cells[5,1]; exit; end; end;//*************************add start****************************** // int k:=0; //int l:=0; Try ExcelApplication1.Connect;//EXCEL应用程序 Except MessageDlg('Excel may not be installed',mtError, [mbOk], 0); Abort; End; try excelapplication1.Workbooks.Open(opendialog1.FileName,null,null,null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件 except begin ExcelApplication1.Disconnect;//出现异常情况时关闭 ExcelApplication1.Quit;showmessage('请选择EXCEL电子表格!'); exit; end; end; ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);//ExcelWorkbook1与Eexcelapplication1建立连接 ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);//Excelworksheet1与Excelworkbook1建立连接 ExcelWorkbook2.ConnectTo(ExcelApplication1.Workbooks[1]);//ExcelWorkbook2与Eexcelapplication1建立连接 ExcelWorksheet2.ConnectTo(ExcelWorkbook2.Worksheets[2] as _Worksheet);//Excelworksheet2与Excelworkbook2建立连接//开始从EXCEL中取数,放到stringgrid1中,取完数后关闭EXCEL for k:=1 to 1000 do//最大取值1000 for l:=1 to 6 do begin if trim(excelworksheet2.cells.item[k+1,1])<>'' then begin stringgrid2.rowCount:=k+1; stringgrid2.Cells[l,k]:=ExcelWorksheet2.Cells.Item[k+1,l]; end else begin // label3.caption:=inttostr(i-1); ExcelApplication1.Disconnect; ExcelApplication1.Quit; //将第一条数据赋给编辑框 memo2.Lines[0]:=stringgrid2.Cells[1,1]; memo2.Lines[1]:=stringgrid2.Cells[2,1]; memo2.Lines[2]:=stringgrid2.Cells[3,1]; memo2.Lines[3]:=stringgrid2.Cells[4,1]; memo2.Lines[4]:=stringgrid2.Cells[5,1]; memo2.Lines[5]:='*************'; exit;//*************************add end****************************** end; end;end;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~问题是add start下面的那些代码都不执行,请教大虾们怎么处理,谢谢