请问各位:怎样在Delphi中控制打开已有的Excel文件啊?不用OLE行不行啊?我想直接打开Excel文件及Excel的界面啊。谢谢

  • 主题发起人 fatcatcat
  • 开始时间
F

fatcatcat

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位:怎样在Delphi中控制打开已有的Excel文件啊?不用OLE行不行啊?我想直接打开Excel文件及Excel的界面啊。谢谢。THX. (100分)<br />请问各位:怎样在Delphi中控制打开已有的Excel文件啊?不用OLE行不行啊?我想直接打开Excel文件及Excel的界面啊。谢谢。THX.
 
用delphi自带的Servers控件。



















 
下面是例子:
//uses Excel2000;

procedure TForm1.Button1Click(Sender: TObject);
begin
with TExcelApplication.Create(Self) do
begin
Connect;
OleVariant(Application).Visible := True;
OleVariant(Workbooks).Open('C:/Book1.xls');
end;
end;
 
用shellapi

shellexecute(handle,'open','aaaaaaa.xls',nil,nil,sw_shownormal);
 
有两种方法应用excel:
1.excelapplication、excelworkbook、excelworksheet控件
应用此种方法打开excel方法如下:
Begin
Try
ExcelApplication1.Connect;
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
ExcelApplication1.Visible[0]:=TRUE;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _Worksheet);
........//对excel表的操作

ExcelApplication1.Quit; //释放excel
End;
2.用createoleobject 方法
Var
ExcelApp,MyWorkBook:Variant;
filename: string;
Begin
Try
ExcelApp:=CreateOleObject('Excel.Application');
Except
MessageDlg('Excel may not be installed',mtError, [mbOk], 0);
Abort;
End;
filename:='d:/book.xls'; //要打开的excel文件
ExcelApp.workBooks.Open(filename);
MyworkBook1:=ExcelApp.workBooks[1];
......//对excel进行操作

MyWorkBook1.saveas(filename);
MyWorkBook1.close; //保存操作

ExcelApp.Quit;
ExcelApp:=Unassigned; //释放excel
End;
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
920
DelphiTeacher的专栏
D
顶部