excel的问题(50分)

  • 主题发起人 主题发起人 Jenky.w
  • 开始时间 开始时间
J

Jenky.w

Unregistered / Unconfirmed
GUEST, unregistred user!
我是想将查询得到的数据直接保存指定在一个Excel文件中,之后操作者才<br>去找到这个文件,点击后打开<br><br>以下这段程序都不能实现我想要的功能,请知道的帮我修改一下<br><br>问题是它要弹出两次保存的窗口,excel应用程序还会闪过了,才消失<br>这都是我不需要的,<br>我要的是<br>指定文件—&gt;保存<br>多谢!!<br><br>if savedialog1.Execute then<br>&nbsp;begin<br>&nbsp; &nbsp; ExcelID := CreateOleObject('Excel.Application');<br>&nbsp; &nbsp; ExcelID.Caption := '应用程序调用 Microsoft Excel';<br>&nbsp; &nbsp; ExcelID.WorkBooks.Add;<br>&nbsp; &nbsp; ExcelID.Cells[1,2].Value := '第一行第四列';<br>&nbsp; Excelid.save(savedialog1.FileName);<br><br>&nbsp; end;<br>&nbsp; Excelid.quit;
 
var Excelid,workbook:olevariant;<br>begin<br>&nbsp; &nbsp;ExcelID := CreateOleObject('Excel.Application');<br>&nbsp; &nbsp;ExcelID.Caption := '应用程序调用 Microsoft Excel';<br>&nbsp; &nbsp;workbook:=ExcelID.WorkBooks.Add;<br>&nbsp; &nbsp;ExcelID.Cells[1,2].Value := '第一行第四列';<br>&nbsp; &nbsp;workbook.saveas('c:/b.xls');<br>&nbsp; &nbsp;workbook.Close;<br>&nbsp; &nbsp;ExcelID.quit;
 
interface<br>&nbsp;uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ComObj, Excel97, StdCtrls, DB, ADODB, OleServer, &nbsp;Oledb;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp;Excelida,workbook:olevariant;<br>begin<br>&nbsp;if savedialog1.Execute then<br>&nbsp; begin<br>&nbsp; &nbsp; ExcelIDa := CreateOleObject('Excel.Application');<br>&nbsp; &nbsp; ExcelIDa.Caption := '应用程序调用 Microsoft Excel';<br>&nbsp; &nbsp; workbook:= ExcelIDa.WorkBooks.Add;<br>&nbsp; &nbsp; ExcelIDa.Cells[1,2].Value := '第一行第四列';<br>&nbsp; &nbsp; workbook.saveas(savedialog1.FileName);<br>&nbsp; &nbsp; workbook.close;<br>&nbsp; end;<br>&nbsp; Excelid.quit;<br>end;<br><br>兄弟,我将程序改成这样,但运行时出错的提示是<br>variant does not reference an automation object.<br><br>
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, DB, DBTables, Grids, DBGrids, Buttons,comobj;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; DBGrid1: TDBGrid;<br>&nbsp; &nbsp; Table1: TTable;<br>&nbsp; &nbsp; DataSource1: TDataSource;<br>&nbsp; &nbsp; SpeedButton1: TSpeedButton;<br>&nbsp; &nbsp; SpeedButton2: TSpeedButton;<br>&nbsp; &nbsp; SpeedButton3: TSpeedButton;<br>&nbsp; &nbsp; procedure SpeedButton1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure SpeedButton2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure SpeedButton3Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; varExcel:Variant;<br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.SpeedButton1Click(Sender: TObject);//打开EXCEL<br><br>begin<br>&nbsp; varExcel:=Createoleobject('Excel.Application');<br>&nbsp; varExcel.Visible:=True;<br>&nbsp; if not varIsEmpty(varExcel)then<br>&nbsp; &nbsp;Begin<br>&nbsp; &nbsp; VarExcel.Workbooks.Add;<br>&nbsp; &nbsp; varExcel.Workbooks[1].Worksheets[1].name:='数据库';<br>// &nbsp; &nbsp;PrintBtn.enabled:=True;<br>&nbsp; &nbsp; Application.BringToFront;<br>&nbsp; &nbsp;end;<br>// &nbsp; except<br>// &nbsp; &nbsp;showmessage('找不到Microsoft Excel');<br>// &nbsp; end;<br>end;<br><br>procedure TForm1.SpeedButton2Click(Sender: TObject);//导出数据<br>Var<br>&nbsp;i,j:integer;<br>&nbsp;bookmark:Tbookmark;<br>begin<br>&nbsp;table1.DisableControls;<br>&nbsp; Begin<br>&nbsp; &nbsp;For i:=0 to table1.FieldCount-1 do<br>&nbsp; &nbsp; varExcel.workbooks[1].worksheets[1].cells[1,i+1].value:=table1.fields.Displaylabel;<br>&nbsp; end;<br>&nbsp; try<br>&nbsp; &nbsp;bookmark:=table1.GetBookmark;<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; table1.First;<br>&nbsp; &nbsp; j:=2;<br>&nbsp; &nbsp; while not table1.Eof do<br>&nbsp; &nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; For i:=0 to table1.fieldcount-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp;Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; varExcel.workbooks[1].worksheets[1].cells[j,i+1].value:=table1.fields.asstring;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;table1.Next;<br>&nbsp; &nbsp; &nbsp; &nbsp;j:=j+1;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;Finally<br>&nbsp; &nbsp; &nbsp; table1.GotoBookmark(bookmark);<br>&nbsp; &nbsp; &nbsp; table1.FreeBookmark(bookmark);<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; table1.EnableControls;<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>procedure TForm1.SpeedButton3Click(Sender: TObject);//关闭保存<br>begin<br>&nbsp;if not varIsEmpty(varExcel)then<br>&nbsp; Begin<br>&nbsp; &nbsp;varExcel.DisplayAlerts:=False;<br>&nbsp; &nbsp;varExcel.Quit;<br>&nbsp; end;<br>end;
 
后退
顶部