delphi導出數據至Excel的七種方法
delphi導出數據至Excel的七種方法
2012-01-17 00:00
一; uses ComObj,clipbrd; function ToExcel(sfilename:string; ADOQuery:TADOQuery):boolean; begin Result := true; try excel:=CreateOleObject(‘Excel.Application‘); excel.workbooks.add; except //screen.cursor:=crDefault; showmessage(‘無法調用Excel!‘); exit; end; savedialog:=tsavedialog.Create(nil); savedialog.FileName:=sfilename; //存入文件 savedialog.Filter:=‘Excel文件(*.xls)|*.xls‘; if savedialog.Execute then begin if FileExists(savedialog.FileName) then try if application.messagebox(‘該文件已經存在,要覆蓋嗎?‘,‘詢問‘,mb_yesno+mb_iconquestion)=idyes then DeleteFile(PChar(savedialog.FileName)) else begin Excel.Quit; savedialog.free; //screen.cursor:=crDefault; Exit; end; except Excel.Quit; savedialog.free; screen.cursor:=crDefault; Exit; end; filename:=savedialog.FileName; end; savedialog.free; if filename=‘‘ then begin result:=true; Excel.Quit; //screen.cursor:=crDefault; exit; end; aSheet:=excel.Worksheets.Item[1]; tsList:=TStringList.Create; //tsList.Add(‘查詢結果‘); //加入標題 s:=‘‘; //加入字段名 ADOQuery.next;
------------------------------------------------------------------------------------------------- 首先在Uses處加上ComObj procedure TForm1.Button1Click(Sender: TObject); ----------------------------------------------------------------------------------------------------------------------------------------------- uses Var
Temsheet.Select; range:=Temsheet.Range[Temsheet.cells[1,1],Temsheet.cells[5,2]];//選定表格 tmpstr:=ExtractFilePath(ParamStr(0))+‘tem.jpg‘; //添加圖片
range:=Temsheet.Range[Temsheet.cells[4,3],Temsheet.cells[4,4]];//選定表格 range:=Temsheet.Range[Temsheet.cells[2,5],Temsheet.cells[2,6]];//選定表格 Temsheet.Cells[3,5]:=‘聯系人:‘; range:=Temsheet.Range[Temsheet.cells[6,1],Temsheet.cells[6,10]];//選定表格 range:=Temsheet.Range[Temsheet.cells[7,1],Temsheet.cells[7,2]];//選定表格 range:=Temsheet.Range[Temsheet.cells[7,3],Temsheet.cells[7,10]];//選定表格 Temsheet.Cells[8,1]:=‘序號‘;
////////////////////////////////////////////// for i:=0 to DBGrid1.Columns.Count - 1 do TemInt:=9+ DBGrid1.DataSource.DataSet.RecordCount; range:=Temsheet.Range[Temsheet.cells[TemInt,1],Temsheet.cells[TemInt,10]];//選定表格 TemInt:=TemInt+1; range:=Temsheet.Range[Temsheet.cells[TemInt,1],Temsheet.cells[TemInt,2]];//選定表格 TemInt:=TemInt+1; Temsheet.Cells[TemInt,1]:=‘序號‘;
TemInt:=TemInt+1; for i:=0 to DBGrid2.Columns.Count - 1 do TemInt:=TemInt+ DBGrid2.DataSource.DataSet.RecordCount; TemInt:=TemInt+1; TemInt:=TemInt+1; range:=Temsheet.Range[Temsheet.cells[TemInt,1],Temsheet.cells[TemInt,2]];//選定表格 TemInt:=TemInt+1; range:=Temsheet.Range[Temsheet.cells[TemInt,1],Temsheet.cells[TemInt,2]];//選定表格 TemInt:=TemInt+1; range:=Temsheet.Range[Temsheet.cells[TemInt,1],Temsheet.cells[TemInt,2]];//選定表格 Application.ProcessMessages; Screen.Cursor:=CrDefault; -------------------------------------------------------------------------------------------------------------------- 四; try
方法六:使用OLE方法導入。 先講TDateSet中的數據保存為二維OLEVariant數組中,再保存到Excel Sheet中 ///使用OLE方式保存procedure TForm1.btn_OleVariantClick(Sender: TObject);
方法七:現在最流行的文件流方法 implementation procedure incColRow; //增加行列號 procedure WriteStringCell(AValue: string);//寫字符串數據 procedure WriteIntegerCell(AValue: integer);//寫整數 procedure WriteFloatCell(AValue: double );//寫浮點數 Procedure ExportExcelFile(FileName: string; bWriteTitle: Boolean; aDataSet: TDataSet); //...... //調用: |
delphi導出數據至Excel的七種方法