T
txzhf
Unregistered / Unconfirmed
GUEST, unregistred user!
源码如下:<br> SaveFn:String;<br> SaveDialog1: TSaveDialog;<br> ExcelApplication1: TExcelApplication;<br> ExcelWorksheet1: TExcelWorksheet;<br> ExcelWorkbook1: TExcelWorkbook;<br> i,row,column :integer;<br> xlsheet,range: Variant;<br>begin<br> SaveDialog1:=TSaveDialog.Create(Application.Owner);<br> SaveDialog1.Filter:='*.Xls';<br> if Not SaveDialog1.Execute then SaveDialog1.Execute ;<br> SaveFn:=SaveDialog1.FileName ;<br> if SaveFn='' then<br> begin<br> messagedlg('请输入文件名!',mtinformation,[mbok],0);<br> abort ;<br> end;<br> ExcelApplication1:=TExcelApplication.Create(Application.Owner);<br> ExcelWorksheet1:=TExcelWorksheet.Create(Application.Owner <br> ExcelWorkbook1:=TExcelWorkbook.Create(Application.Owner <br> try<br> ExcelApplication1.Connect ;<br> except<br> MessageDlg('请确定是否正确安裝Excel2003?',mterror,[mbok],0);<br> abort;<br> end;<br> ExcelApplication1.Visible[0]:=False;<br><br> ExcelApplication1.Caption:='营业收费系统导出数据';<br> ExcelApplication1.Workbooks.Add(Null,0);<br> ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);<br> ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);<br><br> qry1.First ;<br> for i:=1 to qry1.FieldCount do<br> begin<br> ExcelWorksheet1.Cells.Item[1,i]:=qry1.Fields[i-1].DisplayLabel ;<br> end;<br> row:=2;<br> while not qry1.Eof do<br> begin<br> column:=1;<br> for i:=1 to qry1.FieldCount do<br> begin<br> ExcelWorksheet1.Cells.Item[row,column]:=qry1.Fields[i-1].AsString ;<br> column:=column+1;<br> end;<br> row:=row+1;<br> qry1.Next ;<br> end;<br><br> //ExcelApplication1.Visible[0]:=True;<br><br> xlsheet:=ExcelWorkbook1.ActiveSheet ;<br> xlsheet.PageSetup.PrintGridLines := True ;<br> // ExcelApplication1.ActiveWorkBook.Saved := True;<br> range:=xlsheet.range[xlsheet.cells[1,1],xlsheet.cells[qry1.RecordCount+1 ,qry1.FieldCount]];<br> range.font.size:=12;<br> range.HorizontalAlignment:=xlGeneral;<br> range.VerticalAlignment:=xlGeneral;<br> // range.AutoFit;<br> range.Borders[1].LineStyle:=1;<br> range.Borders[2].LineStyle:=1;<br> range.Borders[3].LineStyle:=1;<br> range.Borders[4].LineStyle:=1;<br> ExcelWorkBook1.SaveCopyAs(SaveFn+'.XLS');<br> excelWorkbook1.Close;<br> excelapplication1.Disconnect;<br> excelapplication1.Free;