procedure TForm1.BitBtn1Click(Sender: TObject);
var
xlsFilename :string;
ExcelApp,WorkBook : variant ;
FieldCount : Integer;
sFileName:String;
SaveChanges,Filename: OleVariant;
lLCID,i,j:Integer;
begin
BitBtn1.Enabled := False;
FieldCount := dbgrid1.Columns.Count;
try
ExcelApp := CreateOleObject('Excel.Application');
//WorkBook :=CreateOleObject('Excel.Sheet');
except
Application.MessageBox('您的系统没有安装MicroSoft EXCEL软件,数据不能导出!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
BitBtn1.Enabled := True;
exit;
end; if SaveDialog1.Execute then
begin
xlsFilename := SaveDialog1.FileName;
if FileExists(xlsFilename) then
begin
if Application.MessageBox('该文件已经存在,是否覆盖?','确认',MB_ICONQUESTION+MB_YESNO)=IDYES then
DeleteFile(xlsFilename)
else
begin
BitBtn1.Enabled := True;
exit;
end;
end;
end
else
begin
BitBtn1.Enabled := True;
exit;
end; try
try
ExcelApp:=ExcelApp.WorkBooks.Open( 'C:\aa.xls' );
WorkBook :=ExcelApp.WorkBooks.Add ;
for i:=1 to table1.RecordCount do
begin
for j:=1 to FieldCount do //转化一个记录
begin
ExcelApp.Cells.Item[i+1,j]:=dbgrid1.Fields[j-1].AsString ;
end;
table1.Next ;
end; try
WorkBook.saveas(xlsFilename);
Application.MessageBox('保存完毕!','提示',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);
end;
finally
ExcelApplication1.Quit;
//ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
end;
end;
var
xlsFilename :string;
ExcelApp,WorkBook : variant ;
FieldCount : Integer;
sFileName:String;
SaveChanges,Filename: OleVariant;
lLCID,i,j:Integer;
begin
BitBtn1.Enabled := False;
FieldCount := dbgrid1.Columns.Count;
try
ExcelApp := CreateOleObject('Excel.Application');
//WorkBook :=CreateOleObject('Excel.Sheet');
except
Application.MessageBox('您的系统没有安装MicroSoft EXCEL软件,数据不能导出!','错误提示',MB_OK+MB_SystemModal+MB_IconStop);
BitBtn1.Enabled := True;
exit;
end; if SaveDialog1.Execute then
begin
xlsFilename := SaveDialog1.FileName;
if FileExists(xlsFilename) then
begin
if Application.MessageBox('该文件已经存在,是否覆盖?','确认',MB_ICONQUESTION+MB_YESNO)=IDYES then
DeleteFile(xlsFilename)
else
begin
BitBtn1.Enabled := True;
exit;
end;
end;
end
else
begin
BitBtn1.Enabled := True;
exit;
end; try
try
ExcelApp:=ExcelApp.WorkBooks.Open( 'C:\aa.xls' );
WorkBook :=ExcelApp.WorkBooks.Add ;
for i:=1 to table1.RecordCount do
begin
for j:=1 to FieldCount do //转化一个记录
begin
ExcelApp.Cells.Item[i+1,j]:=dbgrid1.Fields[j-1].AsString ;
end;
table1.Next ;
end; try
WorkBook.saveas(xlsFilename);
Application.MessageBox('保存完毕!','提示',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);
end;
finally
ExcelApplication1.Quit;
//ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货