着急,excel只显示了一个Excel工作平台的菜单条,内容出不来!!!(100分)

  • 主题发起人 主题发起人 山泉
  • 开始时间 开始时间

山泉

Unregistered / Unconfirmed
GUEST, unregistred user!
excel 为什么只显示了一个Excel工作平台的菜单条,
文件内容显示不出来,手动打开test.xls文件的确写进去了,
ShellExecute 应该能显示的,这段代码错在哪里,谢谢。
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
PathName: string;
oldcur: tcursor;
FExcelApplication: TExcelApplication;
FExcelWorkbook: TExcelWorkbook;
FExcelWorksheet: TExcelWorksheet;
FBlnExcelVisible: Boolean;
LCID: Integer;
begin
LCID := LOCALE_USER_DEFAULT;
PathName := ExtractFilePath(Application.ExeName);
if FileExists(pathname + 'test.xls') then
try
DeleteFile(pathname + 'test.xls');
except
end;
if queryprint.Active = true then queryprint.Close;
queryprint.CommandText := 'select * from mainwork where 1=1';
queryprint.open;
begin
try
FExcelApplication := TExcelApplication.Create(Self);
FExcelWorkbook := TExcelWorkbook.Create(Self);
FExcelWorkSheet := TExcelWorksheet.Create(Self);
FBlnExcelVisible := False;

except
showmessage('对不起,您没有安装Excel 2000!');
abort;
end;
end;
begin

FExcelApplication.Connect;

FExcelApplication.Workbooks.Add(Null, 0);
FExcelWorkBook.ConnectTo(FExcelApplication.Workbooks[1]);
FExcelWorkSheet.ConnectTo(FExcelWorkBook.Sheets[1] as _WorkSheet);
end;

//写入execl
if Queryprint.RecordCount > 0 then
begin
for i := 1 to (Queryprint.RecordCount) do
begin
try
FExcelWorkSheet.cells.Item[i + 2, 2] := Queryprint.fieldbyname('文件编号').asstring;
FExcelWorkSheet.cells.Item[i + 2, 3] := Queryprint.fieldbyname('责任者').asstring;
except
end;
Queryprint.Next;
end;
end;

FExcelWorkBook.SaveCopyAs(pathname + 'test.xls', TOleEnum(xlWorkbookNormal));
if queryprint.Active = true then queryprint.Close;
FExcelApplication.DisplayAlerts[LCID] := False;
FExcelApplication.Quit;
FExcelWorksheet.Disconnect;
FExcelWorkbook.Disconnect;
FExcelApplication.Disconnect;
FExcelWorksheet.Free;
FExcelWorkbook.Free;
FExcelApplication.Free;
ShellExecute(Handle, 'Open', PChar(pathname + 'test.xls'), nil, nil, sw_shownormal);
end;
 
代码中写入excel中for语句判断错误;请问queryprint是什么控件的命名,在你的程序中没有定义。
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
PathName: string;
oldcur: tcursor;
FExcelApplication: TExcelApplication;
FExcelWorkbook: TExcelWorkbook;
FExcelWorksheet: TExcelWorksheet;
FBlnExcelVisible: Boolean;
LCID: Integer;
begin
LCID := LOCALE_USER_DEFAULT;
PathName := ExtractFilePath(Application.ExeName);
if FileExists(pathname + 'test.xls') then
try
DeleteFile(pathname + 'test.xls');
except
end;
if queryprint.Active = true then queryprint.Close;
queryprint.CommandText := 'select * from mainwork where 1=1';
queryprint.open;
begin
try
FExcelApplication := TExcelApplication.Create(Self);
FExcelWorkbook := TExcelWorkbook.Create(Self);
FExcelWorkSheet := TExcelWorksheet.Create(Self);
FBlnExcelVisible := False;

except
showmessage('对不起,您没有安装Excel 2000!');
abort;
end;
end;
begin

FExcelApplication.Connect;

FExcelApplication.Workbooks.Add(Null, 0);
FExcelWorkBook.ConnectTo(FExcelApplication.Workbooks[1]);
FExcelWorkSheet.ConnectTo(FExcelWorkBook.Sheets[1] as _WorkSheet);
end;

//写入execl
if Queryprint.RecordCount > 0 then
begin
for i := 1 to (Queryprint.RecordCount-1) do
begin
try
FExcelWorkSheet.cells.Item[i + 2, 2] := Queryprint.fieldbyname('文件编号').asstring;
FExcelWorkSheet.cells.Item[i + 2, 3] := Queryprint.fieldbyname('责任者').asstring;
except
end;
Queryprint.Next;
end;
end;

FExcelWorkBook.SaveCopyAs(pathname + 'test.xls', TOleEnum(xlWorkbookNormal));
if queryprint.Active = true then queryprint.Close;
FExcelApplication.DisplayAlerts[LCID] := False;
FExcelApplication.Quit;
FExcelWorksheet.Disconnect;
FExcelWorkbook.Disconnect;
FExcelApplication.Disconnect;
FExcelWorksheet.Free;
FExcelWorkbook.Free;
FExcelApplication.Free;
ShellExecute(Handle, 'Open', PChar(pathname + 'test.xls'), nil, nil, sw_shownormal);
end;
 
queryprint 我是 TForm1.Button1Click(Sender: TObject)的外面有定义

TForm1 = class(TForm)
queryprint: TADODataSet;
程序编译都通过了,问题是在
ShellExecute(Handle, 'Open', PChar(pathname + 'test.xls'), nil, nil, sw_shownormal);
只显示了一个Excel工作平台的菜单条 ,去掉ShellExecute,内存中已经不存在excel进程,excel按理应该能出来的,奇怪!


 
这种问题我也遇到过,一般是系统内还有Excel的进程未释放,
出现这个问题后,你运行后看看你系统的当前进程内是否存在几个Excel进程
 
接受答案了.
 
wjiachun:
请教您一下,这个问题您解决了吗?
是怎么解决的?谢谢!
我也遇到这个问题了,原因是已经有EXCEL进程存在了。
 
后退
顶部