怎么实现把 adoquery或dbgrideh里的数据导出到EXCEL里面(excel已经存在3列表头) ( 积分: 100 )

  • 主题发起人 主题发起人 fhuibo
  • 开始时间 开始时间
F

fhuibo

Unregistered / Unconfirmed
GUEST, unregistred user!
excel里面已经存在了3列,如下:<br>dwid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWMC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ZCXZ&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>单位ID&nbsp;&nbsp;单位名称&nbsp;&nbsp;注册性质<br>A0100&nbsp;&nbsp;&nbsp;A0200-1&nbsp;&nbsp;&nbsp;A0200-2<br>要导入的数据
 
看看这个吧<br>procedure&nbsp;TstartForm.SpeedButton10Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;var&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;MSExcel:Variant;<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;MSExcel:=CreateOLEObject('Excel.Application');<br>&nbsp;&nbsp;MSExcel.WorkBooks.Add;<br>&nbsp;&nbsp;MSExcel.Visible:=True;<br><br><br><br>&nbsp;&nbsp;for&nbsp;i:=5&nbsp;to&nbsp;Query1.RecordCount+4&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;MsExcel.Cells[i,1].value:=Query1.FieldByName('cpxhno').Asstring;<br>&nbsp;&nbsp;MsExcel.Cells[i,2].value:=Query1.FieldByName('cpname').Asstring;<br>&nbsp;&nbsp;MsExcel.Cells[i,3].value:=Query1.FieldByName('cpunit').Asstring;<br>&nbsp;&nbsp;MsExcel.Cells[i,4].value:=Query1.FieldByName('htnumber').Asstring;<br>&nbsp;&nbsp;MsExcel.Cells[i,5].value:=Query1.FieldByName('number').Asstring;<br>&nbsp;&nbsp;MsExcel.Cells[i,6].value:=Query1.FieldByName('ljno').Asstring;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end;<br><br>退出Excel:<br>MSExcel.Quit;<br>MSExcel:=Unassigned;&nbsp;//释放VARIANT变<br>***************<br>先加入ExcelApplication1;ExcelWorkbook1;ExcelWorksheet1;<br>procedure&nbsp;TForm1.n1Click(Sender:&nbsp;TObject);<br>var<br>&nbsp;&nbsp;row,brea:integer;<br>&nbsp;&nbsp;a,b,c,d,e:shortstring;<br>begin<br>&nbsp;&nbsp;if&nbsp;table1.active&nbsp;=&nbsp;True&nbsp;then&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExcelApplication1.Connect;<br>&nbsp;&nbsp;&nbsp;&nbsp;Except<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExcelApplication1.free;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Abort;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;brea&nbsp;:=&nbsp;strtoint(inputbox('输入需导入行数!','行数','10'));<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;brea&lt;1&nbsp;then&nbsp;brea&nbsp;:=&nbsp;10;<br>&nbsp;&nbsp;&nbsp;&nbsp;ProgressBar1.Visible&nbsp;:=&nbsp;True;<br>&nbsp;&nbsp;&nbsp;&nbsp;ProgressBar1.Max&nbsp;:=&nbsp;brea;<br>&nbsp;&nbsp;&nbsp;&nbsp;ExcelApplication1.Visible[0]&nbsp;:=&nbsp;true;<br>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;row&nbsp;:=&nbsp;2&nbsp;to&nbsp;brea&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;:=&nbsp;ExcelWorksheet1.Cells.Item[row,2];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;:=&nbsp;ExcelWorksheet1.Cells.Item[row,1];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;:=&nbsp;ExcelWorksheet1.Cells.Item[row,3];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d&nbsp;:=&nbsp;ExcelWorksheet1.Cells.Item[row,4];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e&nbsp;:=&nbsp;ExcelWorksheet1.Cells.Item[row,5];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;form1.show;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;table1.AppendRecord([a,b,c,d,e]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ProgressBar1.Position&nbsp;:=&nbsp;row-1;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;ProgressBar1.Hide;<br>&nbsp;&nbsp;end;<br>end;<br>*****************<br>var<br>MSExcel:&nbsp;Variant;<br>i:&nbsp;Integer;<br>begin<br>OpenDialog1.Filter:='*.XLS|*.XLS';<br>OpenDialog1.DefaultExt:='XLS';<br>if&nbsp;OpenDialog1.Execute&nbsp;then<br>begin<br>&nbsp;&nbsp;MSExcel:=CreateOLEObject('Excel.Application');<br>&nbsp;&nbsp;MSExcel.WorkBooks.Open(OpenDialog1.FileName);<br>&nbsp;&nbsp;MSExcel.Visible:=False;<br>&nbsp;&nbsp;//从有数据的行逐行读入数据<br>&nbsp;&nbsp;for&nbsp;i:=1&nbsp;to&nbsp;MSExcel.ActiveSheet.UsedRange.Rows.Count&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Edit2.Text:=Edit2.Text+MSExcel.Cells[i,1].Value;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;MSExcel.ActiveWorkBook.Close;<br>&nbsp;&nbsp;MSExcel.Quit;<br>end;<br>end;
 
顶一个,接个分
 
记得有个第三方控件的,可惜名字忘记了。<br>早先做过,很容易上手的......不知道楼下的人还有什么高见
 
uses&nbsp;DBGridEhImpExp<br>SaveDBGridEhToExportFile(TDBGridEhExportAsXLS,&nbsp;DBGridEh1,&nbsp;SaveDialog1.FileName,True);
 
后退
顶部