详见:http://community.csdn.net/Expert/topic/4126/4126701.xml?temp=.62875
var
afile:textfile;
fname;string;
if strexcel<>'' then begin
tempdialog := tsavedialog.Create(nil);
tempdialog.Filter:='*.txt|*.txt';
tempdialog.Execute;
fname:=tempdialog.FileName;
if copy(fname,strlen(pchar(fname))-4,3)<>'txt' then fname:=fname+'.txt';
showmessage(fname);
if not FileExists(fname) then filecreate(fname);
assignfile(afile,fname);//这里出错
reset(afile);
append(afile);
writeln(afile,strexcel);
flush(afile);
closefile(afile);
shellexecute(0,'Open',pchar(fname),nil,nil,1);
end;
正确答案:
tempdialog := tsavedialog.Create(nil);
tempdialog.Filter:='*.txt|*.txt';
tempdialog.DefaultExt := 'xls';
tempdialog.Execute;
AssignFile(afile, tempdialog.FileName); //改这样就好了,不知道为什么?
rewrite(afile);
write(afile, strexcel);
closefile(afile);
ShellExecute(0, nil, pchar(tempdialog.FileName), nil, nil, 1);