如何用delphi5 自已的元件 excelapplication 打开一个已有XLS文件及其他?(100分)

  • 主题发起人 主题发起人 wshuming
  • 开始时间 开始时间
W

wshuming

Unregistered / Unconfirmed
GUEST, unregistred user!
1. 如标题
2.并对指定名字的工作表操作
3.然后存盘退出?
 
ExcelApplication.Connect;
// ExcelApplication1.Visible[0]:=true;
{打开文件}
ExcelApplication.Workbooks.Add(ExtractFilePath(application.exename)+'卷内文件模版.xlt',0);
ExcelWorkbook1.ConnectTo(ExcelApplication.Workbooks[1]);
// ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[1] as _WorkSheet);
//write the field name
{ for idx:=0 to DB.Fields.Count-1 do
{向excel填入数据}
ExcelWorksheet1.Cells.Item[1,idx+1]:=DB.Fields[idx].FieldName;}
recordno:=3;
PageNo:=0;
While Not Table.Eof do
begin
if recordno=3 then//the new page is start...
begin
{增加worksheet}
pageno:=pageno+1;
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[excelapplication.sheets.count] as _WorkSheet);
excelworksheet1.Copy(excelworkbook1.sheets[excelapplication.sheets.count] as _worksheet);
excelworksheet1.ConnectTo(excelworkbook1.sheets[excelapplication.sheets.count-1] as _worksheet);
excelworksheet1.name:='第'+IntToStr(PageNo)+'页';
end;
for idx:=0 to Table.Fields.Count-1 do
ExcelWorksheet1.Cells.Item[recordno,idx+1]:=Table.Fields[idx].AsString;

if recordno<12 then
recordno:=recordno+1
else
recordno:=3;
Table.Next;
end;

excelworksheet1.ConnectTo(excelworkbook1.sheets[excelapplication.sheets.count] as _worksheet);
excelworksheet1.Visible[0]:=0;
//free the excel application
{保存}
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWorkBook1.Close(false);
ExcelApplication.Disconnect;
{关闭excel}
ExcelApplication.Quit;
 
1. '卷内文件模版.xlt' ? 为什么不是 xls 文件

2. ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName); 我不想另存为只要重新存盘


 
1.xlt是excel的模班,我应用了excel的模班功能。当然这里也可以是xls
2.excelworkbook1.save 就可以了或savecopyas(同一个名字)也可以
 

1.我用了 excelworkbook1.save; 我原先在 e:/app/下的文件存到了c:/my documents 下了
而且文件名后多了如原是e:/app/aaa.xls 变成了c:/my documents/aa1.xls

2. 如果我原先打开的是aaa.xls是不是用
ExcelWorkBook1.SaveCopyAs(ExtractFilePath(application.exename)+'aaa.xls');
3. 我要对指定的表操作,如aaa.xls 有5张表格,其中一张名为 "BBB"
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[1] as _WorkSheet)
此句不知联到哪张表上好象是 打开的表中序号为1的表格
还请详解?
 
1.想象一下你编辑一个excel模班文件的时候,你按一下保存会怎么样就明白了
2.xlt和xls有一点区别。自己使用一下excel这个软件你就明白
3.指你第一张worksheet
详悉情况请留意excel vba (off97 的help)
 
在Delphi 5中简单地封装了一组Microsoft Office自动化对象(Automation servers)。它使得我们很容
易地把Office中的应用程序(Word, Excel, PowerPoint, Outlook and Access等)当作一个com应用服务器进行控
制。在Delphi 5中已经带了Word与PowerPoint的例子,因为Excel的调用与这两个应用服务器的调用略有不同,
所以本人根据这两个例子写了个Excel 97的简单例子以供参考。

创建一个普通Application。

在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。

连接Excel 97,具体方法如下:
打开Excel97。
Try
ExcelApplication1.Connect;
Except
End;
ExcelApplication1.Visible[0]:=True;
增加一个Workbook。
ExcelWorkbook1.ConnectTo(ExcelApplication1.
Workbooks.Add(EmptyParam,0));

添加一个Worksheet。
var
Temp_Worksheet: _WorkSheet;
begin
Try
Temp_Worksheet:=ExcelWorkbook1.
WorkSheets.Add(EmptyParam,
EmptyParam,EmptyParam,EmptyParam,0)
as _WorkSheet;//(注意)
ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);
Except
ShowMessage('Failure');
End;
end;
关闭Excel.
Try
ExcelApplication1.Quit;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
Except
End;
4. 对Excel的一些操作:
选择当前Workbook的某一Worksheet.
procedure TForm1.ComboBox1DropDown
(Sender: TObject);
var
i: Integer;
begin
ComboBox1.Clear;
For i:=1 to ExcelWorkbook1.
Worksheets.Count do
ComboBox1.Items.Add
((ExcelWorkbook1.Worksheets.Item
as _WorkSheet).Name);
end;

procedure TForm1.ComboBox1Change
(Sender: TObject);
begin
ExcelWorkSheet1.ConnectTo
(ExcelWorkbook1.Worksheets.Item
[ComboBox1.ItemIndex+1] as _WorkSheet);
ExcelWorkSheet1.Activate;
end;

选择某一Workbook:
procedure TForm1.ComboBox2DropDown
(Sender: TObject);
var
i: Integer;
begin
ComboBox2.Clear;
if ExcelApplication1.Workbooks.Count >0 then
For i:=1 to ExcelApplication1.Workbooks.Count do
Combobox2.Items.Add(ExcelApplication1.
Workbooks.Item.Name);
end;

procedure TForm1.ComboBox2Change(Sender: TObject);
begin
ExcelWorkSheet1.Disconnect;
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks.
Item[Combobox2.ItemIndex+1]);
ExcelWorkBook1.Activate;
ExcelWorksheet1.ConnectTo(ExcelWorkBook1.
ActiveSheet as _WorkSheet);
ExcelWorkSheet1.Activate;
end;

对某一单元格进行赋值及取值。
procedure TForm1.Button5Click(Sender: TObject);
begin
ExcelWorksheet1.Cells.Item[SpinEdit2.Value,
SpinEdit1.Value]:=Edit1.Text;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
Edit1.Text:=ExcelWorksheet1.Cells.Item[
SpinEdit2.Value,SpinEdit1.Value];
end;

选择某一区域
ExcelWorkSheet1.Range['A1','C1'].Select;

打开一个Excel文件。
if OpenDialog1.Execute then
Begin
Try
ExcelWorkBook1.ConnectTo
(ExcelApplication1.Workbooks.Open
(OpenDialog1.FileName,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,
EmptyParam,EmptyParam,0));
ExcelWorkSheet1.ConnectTo
(ExcelWorkBook1.Activesheet
as _Worksheet);
Except;
End;
End;

执行宏调用、保存文件、打印文件及对Excel的设置等,请参阅Microsoft Excel Visual Basic参考中的Microsoft Excel对象
 
多人接受答案了。
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部