山
山泉
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;
文件内容显示不出来,手动打开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;