请问delphi中导出excel是怎么做到的,我这边想把dbgrid中的数据导出到excel
展开全部
方法1: 使用dbgridtoexcel 控件...
方法2:利用adoquery
以下转自网络:
unit ADOQuery2Xls;
interface
uses SysUtils, Classes, DB, ADODB,ComObj,Variants;
type TADOQuery2Xls = class(TADOQuery)
private { Private declarations }
ExcelApp:Variant;
MyWorkBook:Variant;
FOnFinishExportXls:TNotifyEvent; procedure
CreateXlsObj; procedure DestroyXlsObj;
protected { Protected declarations }
public { Public declarations
} constructor Create( AOwner: TComponent
);override; destructor Destroy;override;
published { Published declarations }
property onFinishExportXls:TNotifyEvent read FOnFinishExportXls write
FOnFinishExportXls; procedure
ExportToXls(XlsFileName:string); end;
procedure Register;
implementation
uses Math;
procedure Register;begin RegisterComponents('ADO',
[TADOQuery2Xls]);end;
procedure TADOQuery2Xls.ExportToXls(XlsFileName:string);var
col,row:integer;begin if XlsFileName='' then
exit; CreateXlsObj; if not Active then
Open; DisableControls; row:=1;
for col:=1 to FieldCount do
MyWorkBook.sheets[1].cells[row,col].value:=Fields[col-1].FieldName;
inc(row); First; while not Eof
do begin for col:=1 to
FieldCount do
MyWorkBook.sheets[1].cells[row,col].value:=Fields[col-1].Value;
DBApplication.ProcessMessages;
inc(row); Next;
end; EnableControls;
MyWorkBook.saveas(XlsFileName); DestroyXlsObj;
if Assigned(FOnFinishExportXls) then
FOnFinishExportXls(self);end;procedure
TADOQuery2Xls.CreateXlsObj;begin
try
ExcelApp:=CreateOleObject('Excel.application');
MyWorkBook:=CreateOleObject('Excel.Sheet');
except raise exception.Create('Can not
create Excel application,please make MS Excel is installed in your
computer'); end;
MyworkBook:=ExcelApp.workBooks.Add;end;procedure
TADOQuery2Xls.DestroyXlsObj;begin
MyWorkBook.close; ExcelApp.Quit;
ExcelApp:=Unassigned; // release variantend;constructor
TADOQuery2Xls.create(AOwner: TComponent);begin inherited Create(
AOwner );end;destructor TADOQuery2Xls.Destroy;begin
inherited destroy;end;end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-21
展开全部
多数是使用OLE的,OLE的比较容易但是速度较慢。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-21
展开全部
用EHLIB控件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-03-21
展开全部
导出成csv再给excel不好吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询