我在做EXCEL导入SQL数据库中,EXCEL中有多个表怎么操作?(60分)

  • 主题发起人 主题发起人 computerzzz
  • 开始时间 开始时间
C

computerzzz

Unregistered / Unconfirmed
GUEST, unregistred user!
我在做EXCEL导入SQL数据库中,EXCEL中有多个表,都要导入SQL,可是只能导入第一个工作表,其它的工作表,根本没导入数据库?<br>procedure TCostForm.Button2Click(Sender: TObject);<br>var k,i,j:integer;<br>sheetcount:integer;<br>begin<br>//opendialog1.InitialDir:=ExtractFileDir(paramstr(0));//文件的打存放初始路径<br>opendialog1.Execute;<br>Try<br> &nbsp;ExcelApplication1.Connect;//EXCEL应用程序<br>Except<br> &nbsp;MessageDlg('Excel may not be installed',mtError, [mbOk], 0);<br> &nbsp;Abort;<br>End;<br>ExcelApplication1.Visible[0]:=false;//True; &nbsp;//是否显示EXCEL<br>ExcelApplication1.Caption:='Excel Application';<br>try<br> &nbsp;excelapplication1.Workbooks.Open(opendialog1.FileName,<br> &nbsp;null,null,null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件<br>except<br>begin<br> &nbsp;ExcelApplication1.Disconnect;//出现异常情况时关闭<br> &nbsp;ExcelApplication1.Quit;showmessage('请选择EXCEL电子表格!');<br> &nbsp;exit;<br>end;<br>end;<br>Try<br>//ExcelWorkbook1与Eexcelapplication1建立连接<br>ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);<br>sheetcount:=ExcelWorkbook1.Sheets.Count;<br> //Excelworksheet1与Excelworkbook1建立连接<br><br>for k:=1 to sheetcount+1 do<br>begin<br> ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[k] as _Worksheet);<br>// &nbsp;excelworksheet1.Activate(k);<br>// &nbsp;ExcelApplication1.Workbooks[k].Activate;<br> &nbsp;ExcelApplication1.Workbooks[k].ActiveSheet;<br> &nbsp;adoquery1.Active:=true;<br> &nbsp;for i:=3 to 1000 do &nbsp;//最大取值1000<br> &nbsp;begin<br> &nbsp; &nbsp;if trim(excelworksheet1.Cells.Item[i,1])&lt;&gt;'' then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;adoquery1.Append;<br> &nbsp; &nbsp; &nbsp;for j:=2 to 51 do &nbsp; //<br> &nbsp; &nbsp; &nbsp; &nbsp; adoquery1.Fields[j-2].Value:=ExcelWorksheet1.Cells.Item[i,j];<br> &nbsp; &nbsp; &nbsp;adoquery1.Post;<br> &nbsp; &nbsp;end<br> &nbsp; &nbsp;else exit;<br> &nbsp;end;<br> &nbsp;excelworksheet1.Disconnect;<br>end;<br>finally<br> &nbsp;ExcelApplication1.Disconnect;<br> &nbsp;ExcelApplication1.Quit;<br>end;<br><br>end;
 
后退
顶部