{导出EXCEL begin}
DlgSave:=TsaveDialog.Create(nil);
DlgSave.Filter:='*.xls|*.xls';
repeat
excelbool:=True;
if DlgSave.Execute then
begin
application.ProcessMessages; filename:=DlgSave.FileName;
try
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.Caption :='Excel-cms2002';
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('Export to Excel file failed','Error',mb_OK+mb_IconStop);
exit;
end;
if FileExists(filename)= true then
begin
return:=application.messagebox( ERR_MSG_FILE_BEING,ERR_TITLE_CNFRMTN, MB_YESNOCANCEL or mb_iconquesTion);
IF return=ID_YES THEN
BEGIN
errormsg.zpMsgForm.zpMessageBox(ERR_MSG_EXCEL);
MyworkBook:=ExcelApp.workBooks.Add(1);
ExcelApp.DisplayAlerts:=False;
excelbool:=True;
END
ELSE if return=ID_NO THEN
BEGIN
excelbool:=false;
END
else if return=ID_CANCEL THEN
EXIT;
end
else begin
errormsg.zpMsgForm.zpMessageBox(ERR_MSG_EXCEL);
MyworkBook:=ExcelApp.workBooks.Add(1);
excelbool:=True;
end;
application.ProcessMessages;
if excelbool= true then
begin
... ...
ExcelApp.Columns.AutoFit;
ExcelApp.ActiveWorkBook.SaveAs(FileName);
ExcelApp.WorkBooks.Close;
errormsg.zpMsgForm. Closemessagebox;
application.MessageBox('Export to Excel file done','Export to Excel',0);
ExcelApp.Quit;
DlgSave.Destroy;
end;
end;
until(excelbool)
{导出 end}
我用以上方法把数据导出excel
为什么当程序执行完之后。内存还在运行excel?
再次打开excel文件时只有工具兰,而内容没有了。
请各为帮帮忙!
DlgSave:=TsaveDialog.Create(nil);
DlgSave.Filter:='*.xls|*.xls';
repeat
excelbool:=True;
if DlgSave.Execute then
begin
application.ProcessMessages; filename:=DlgSave.FileName;
try
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.Caption :='Excel-cms2002';
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('Export to Excel file failed','Error',mb_OK+mb_IconStop);
exit;
end;
if FileExists(filename)= true then
begin
return:=application.messagebox( ERR_MSG_FILE_BEING,ERR_TITLE_CNFRMTN, MB_YESNOCANCEL or mb_iconquesTion);
IF return=ID_YES THEN
BEGIN
errormsg.zpMsgForm.zpMessageBox(ERR_MSG_EXCEL);
MyworkBook:=ExcelApp.workBooks.Add(1);
ExcelApp.DisplayAlerts:=False;
excelbool:=True;
END
ELSE if return=ID_NO THEN
BEGIN
excelbool:=false;
END
else if return=ID_CANCEL THEN
EXIT;
end
else begin
errormsg.zpMsgForm.zpMessageBox(ERR_MSG_EXCEL);
MyworkBook:=ExcelApp.workBooks.Add(1);
excelbool:=True;
end;
application.ProcessMessages;
if excelbool= true then
begin
... ...
ExcelApp.Columns.AutoFit;
ExcelApp.ActiveWorkBook.SaveAs(FileName);
ExcelApp.WorkBooks.Close;
errormsg.zpMsgForm. Closemessagebox;
application.MessageBox('Export to Excel file done','Export to Excel',0);
ExcelApp.Quit;
DlgSave.Destroy;
end;
end;
until(excelbool)
{导出 end}
我用以上方法把数据导出excel
为什么当程序执行完之后。内存还在运行excel?
再次打开excel文件时只有工具兰,而内容没有了。
请各为帮帮忙!
DlgSave : TSaveDialog;
begin
DlgSave:=TsaveDialog.Create(nil);
DlgSave.Filter:='*.xls|*.xls';
DlgSave 已经声明了
改为
ExcelApp.workBooks.Add(xlWBATWorkSheet,0);
MyworkBook:=WorkSheets.Item[1];
试试
pc_i:integer;
xlapp,sheet: variant;
WBK: OleVariant;
begin
xlapp := createoleobject('excel.application');
if Trim(Edit1.Text)='' then
begin
Application.MessageBox('文件名不能为空','警告',MB_OK);
Exit;
end;
WBK := xlapp.workbooks.Open(Edit1.Text);
sheet := xlapp.workbooks[1].worksheets['sheet1'];
pc_i:=1;
Edit2.Text := sheet.cells[pc_i,1]; //写入的话用 sheet.cells[pc_i,1] := Edit2.Tex;
WBK.Close(SaveChanges := True);
xlapp.Quit;
end;
这是读出Excel文件的代码
你用这个好了
你在释放的时候加一段看看
if not varIsEmpty(ExcelApp) then
begin
ExcelApp.DisplayAlert:=True;
ExcelApp.WorkBooks[1].close;
ExcelApp.Quit;
end;
应为
MyworkBook:=ExcelApp.WorkSheets.Item[1];
MyworkBook:=ExcelApp.workBooks.Add(1);
我想请教你,这个是什么意思?
MyWorkBook.Free;
ExcelApp.Free;
ExcelApp := Unassigned;
试试