use comobjprocedure TSCZNPC_Form.Excel_ButtonClick(Sender: TObject); var xlsFilename :string; eclApp,WorkBook :variant ; a_filedNo,i,j :integer; fs : string; p1 : array[0..300] of Char; begin a_filedNo := DBGrid1.FieldCount ; try eclApp :=CreateOleObject('Excel.Application'); WorkBook :=CreateOleObject('Excel.Sheet'); except Application.MessageBox('您的系统没有安装MicroSoft EXCEL软件,数据不能导出!','错误提示',MB_OK+MB_SystemModal+MB_IconStop); exit; end; if SaveDialog1.Execute then xlsFileName := SaveDialog1.FileName else exit; try WorkBook :=eclApp.workBooks.add ; for i :=1 to a_FiledNo do //转化字段名; begin eclApp.cells.item(1,i) := DBGrid1.Fields[i-1].FieldName ; end; DBGrid1.DataSource.DataSet.First ; for i :=1 to DBGrid1.DataSource.DataSet.RecordCount do begin for j :=1 to a_filedNo do //转化一个记录 begin eclApp.cells.item(i+1,j) :=DbGrid1.Fields[j-1].AsString ; end; DBGrid1.DataSource.DataSet.Next ; end; try fs := xlsFileName; StrPCopy(p1,'文件"' + ExtractFilePath(Application.ExeName )+xlsFileName + '"已存在,'+#13+'是否覆盖?'); if FileExists(fs) then begin if Application.MessageBox(p1,'确认',MB_ICONQUESTION+MB_YESNO)=IDYES then DeleteFile(fs) else exit; end; WorkBook.saveas(fs); WorkBook.close; StrPCopy(p1,'EXECL文件已经保存,路径为:' + fs); Application.MessageBox(p1,'提示',MB_OK+MB_SystemModal+MB_IconInformation); except Application.MessageBox('保存文件出现异常!','错误提示',MB_OK+MB_SystemModal+MB_IconStop); end; except Application.MessageBox('不能正确操作EXECL文件,可能该文件已经被其他程序占用或系统错误!','错误提示',MB_OK+MB_SystemModal+MB_IconStop); WorkBook.close; eclApp.quit; eclApp := Unassigned; end;end;
var
xlsFilename :string;
eclApp,WorkBook :variant ;
a_filedNo,i,j :integer;
fs : string;
p1 : array[0..300] of Char;
begin
a_filedNo := DBGrid1.FieldCount ; try
eclApp :=CreateOleObject('Excel.Application');
WorkBook :=CreateOleObject('Excel.Sheet');
except
Application.MessageBox('您的系统没有安装MicroSoft EXCEL软件,数据不能导出!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
exit;
end; if SaveDialog1.Execute then
xlsFileName := SaveDialog1.FileName
else
exit; try
WorkBook :=eclApp.workBooks.add ;
for i :=1 to a_FiledNo do //转化字段名;
begin
eclApp.cells.item(1,i) := DBGrid1.Fields[i-1].FieldName ;
end; DBGrid1.DataSource.DataSet.First ;
for i :=1 to DBGrid1.DataSource.DataSet.RecordCount do
begin
for j :=1 to a_filedNo do //转化一个记录
begin
eclApp.cells.item(i+1,j) :=DbGrid1.Fields[j-1].AsString ;
end;
DBGrid1.DataSource.DataSet.Next ;
end;
try
fs := xlsFileName;
StrPCopy(p1,'文件"' + ExtractFilePath(Application.ExeName )+xlsFileName + '"已存在,'+#13+'是否覆盖?');
if FileExists(fs) then
begin
if Application.MessageBox(p1,'确认',MB_ICONQUESTION+MB_YESNO)=IDYES then
DeleteFile(fs)
else
exit;
end;
WorkBook.saveas(fs);
WorkBook.close;
StrPCopy(p1,'EXECL文件已经保存,路径为:' + fs);
Application.MessageBox(p1,'提示',MB_OK+MB_SystemModal+MB_IconInformation);
except
Application.MessageBox('保存文件出现异常!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
end;
except
Application.MessageBox('不能正确操作EXECL文件,可能该文件已经被其他程序占用或系统错误!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
WorkBook.close;
eclApp.quit;
eclApp := Unassigned;
end;end;
http://www.csdn.net/develop/article/18/18623.shtm
WorkBook :=CreateOleObject('Excel.Sheet');
为什么CreateOleObject这个出错
uses :comobj;