看看这个吧<br>procedure TstartForm.SpeedButton10Click(Sender: TObject);<br> var <br> MSExcel:Variant;<br> begin<br> MSExcel:=CreateOLEObject('Excel.Application');<br> MSExcel.WorkBooks.Add;<br> MSExcel.Visible:=True;<br><br><br><br> for i:=5 to Query1.RecordCount+4 do<br> begin<br> MsExcel.Cells[i,1].value:=Query1.FieldByName('cpxhno').Asstring;<br> MsExcel.Cells[i,2].value:=Query1.FieldByName('cpname').Asstring;<br> MsExcel.Cells[i,3].value:=Query1.FieldByName('cpunit').Asstring;<br> MsExcel.Cells[i,4].value:=Query1.FieldByName('htnumber').Asstring;<br> MsExcel.Cells[i,5].value:=Query1.FieldByName('number').Asstring;<br> MsExcel.Cells[i,6].value:=Query1.FieldByName('ljno').Asstring;<br> end;<br> end;<br><br>退出Excel:<br>MSExcel.Quit;<br>MSExcel:=Unassigned; //释放VARIANT变<br>***************<br>先加入ExcelApplication1;ExcelWorkbook1;ExcelWorksheet1;<br>procedure TForm1.n1Click(Sender: TObject);<br>var<br> row,brea:integer;<br> a,b,c,d,e:shortstring;<br>begin<br> if table1.active = True then begin<br> try<br> ExcelApplication1.Connect;<br> Except<br> ExcelApplication1.free;<br> Abort;<br> end;<br> brea := strtoint(inputbox('输入需导入行数!','行数','10'));<br> if brea<1 then brea := 10;<br> ProgressBar1.Visible := True;<br> ProgressBar1.Max := brea;<br> ExcelApplication1.Visible[0] := true;<br> for row := 2 to brea do<br> begin<br> a := ExcelWorksheet1.Cells.Item[row,2];<br> b := ExcelWorksheet1.Cells.Item[row,1];<br> c := ExcelWorksheet1.Cells.Item[row,3];<br> d := ExcelWorksheet1.Cells.Item[row,4];<br> e := ExcelWorksheet1.Cells.Item[row,5];<br> form1.show;<br> table1.AppendRecord([a,b,c,d,e]);<br> ProgressBar1.Position := row-1;<br> end;<br> ProgressBar1.Hide;<br> end;<br>end;<br>*****************<br>var<br>MSExcel: Variant;<br>i: Integer;<br>begin<br>OpenDialog1.Filter:='*.XLS|*.XLS';<br>OpenDialog1.DefaultExt:='XLS';<br>if OpenDialog1.Execute then<br>begin<br> MSExcel:=CreateOLEObject('Excel.Application');<br> MSExcel.WorkBooks.Open(OpenDialog1.FileName);<br> MSExcel.Visible:=False;<br> //从有数据的行逐行读入数据<br> for i:=1 to MSExcel.ActiveSheet.UsedRange.Rows.Count do<br> begin<br> Edit2.Text:=Edit2.Text+MSExcel.Cells[i,1].Value;<br> end;<br> MSExcel.ActiveWorkBook.Close;<br> MSExcel.Quit;<br>end;<br>end;