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> ExcelApplication1.Connect;//EXCEL应用程序<br>Except<br> MessageDlg('Excel may not be installed',mtError, [mbOk], 0);<br> Abort;<br>End;<br>ExcelApplication1.Visible[0]:=false;//True; //是否显示EXCEL<br>ExcelApplication1.Caption:='Excel Application';<br>try<br> excelapplication1.Workbooks.Open(opendialog1.FileName,<br> null,null,null,null,null,null,null,null,null,null,null,null,null,null,0);//打开指定的EXCEL 文件<br>except<br>begin<br> ExcelApplication1.Disconnect;//出现异常情况时关闭<br> ExcelApplication1.Quit;showmessage('请选择EXCEL电子表格!');<br> 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>// excelworksheet1.Activate(k);<br>// ExcelApplication1.Workbooks[k].Activate;<br> ExcelApplication1.Workbooks[k].ActiveSheet;<br> adoquery1.Active:=true;<br> for i:=3 to 1000 do //最大取值1000<br> begin<br> if trim(excelworksheet1.Cells.Item[i,1])<>'' then<br> begin<br> adoquery1.Append;<br> for j:=2 to 51 do //<br> adoquery1.Fields[j-2].Value:=ExcelWorksheet1.Cells.Item[i,j];<br> adoquery1.Post;<br> end<br> else exit;<br> end;<br> excelworksheet1.Disconnect;<br>end;<br>finally<br> ExcelApplication1.Disconnect;<br> ExcelApplication1.Quit;<br>end;<br><br>end;