高分求DEPHI5+EXECL完全解答(150分)

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

wordboy

Unregistered / Unconfirmed
GUEST, unregistred user!
刚有人问过类似问题
小子愚笨,不甚了解
有详细过程有分
不要告诉我哪本书有答案
 
什么问题?
 
首先建立一個TExcelApplication控件﹐設名為EA1,
var aSheet:variant;
i,j:integer;
begin
EA1.Connect;
EA1.Workbooks.Open('C:/yourexcel.xls',EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,1);
//這是打開某個特定文件
// EA1.Workbooks.Add(xlWBATWorkSheet,0); 直接新增一個Excel文件
EA1.Visible[0]:=False;
aSheet:=EA1.Worksheets.Item[1];
x:=0;
for i:=1 to yourdata.RecordCount do
for j:=1 to yourdata.Fieldcount do
asheet.Cells[i,j].Value:=yourdata.fields[j-1].asstring;
SaveDialog.Execute;
if SaveDialog.FileName<>'noname' then begin
aSheet.SaveAs(SaveDialog.FileName);
ShowMessage('文件已保存為'+SaveDialog.FileName);
end;
aSheet.Application.Quit;
end;
這樣可以把你的數據集資料一筆筆的寫到Excel去。
另外﹐你去community.borland.com/article/014101012800.html﹐里面有相關的資料。
 
咳,来晚了一步
 
用ado和odbc怎么用,我可以连上,但用ttable
连不上表
 
wordboy:如果还想接着讨论请定期提前自己的帖子,如果不想继续讨论请结束帖子。
 
about Delphi 5.0中如何读取Excel 97文件

procedure TOfficeform.Button2Click(Sender: TObject);
var MSExcel:Variant;
i,j:Integer;
begin
SaveDialog1.Filter:='*.XLS¦*.XLS';
SaveDialog1.DefaultExt:='XLS';
if SaveDialog1.Execute then
begin
MSExcel:=CreateOLEObject('Excel.Application');
MSExcel.WorkBooks.Add;
MSExcel.Visible:=False;
Table1.Open;
j:=Table1.RecordCount;
Table1.First;
for i:=1 to j do
begin
MSExcel.Cells[i,1].NumberFormat:='@';
MSExcel.Cells[i,1].Value:=Table1.FieldByName('CODE').AsString;
MSExcel.Cells[i,2].Value:=Table1.FieldByName('COLOR').AsString;
Table1.Next;
end;
MSExcel.ActiveWorkBook.SaveAs(SaveDialog1.FileName);
MSExcel.ActiveWorkBook.Saved:=True;
MSExcel.Quit;
end;
end;


利用Delphi 5中调用Excel 97
---- 在Delphi 5中简单地封装了一组Microsoft Office自动化对象(Automation servers)。它使得我们很容易地把Office中的应用程序(Word, Excel, PowerPoint, Outlook and Access等)当作一个com应用服务器进行控制。在Delphi 5中已经带了Word与PowerPoint的例子,因为Excel的调用与这两个应用服务器的调用略有不同,所以本人根据这两个例子写了个Excel 97的简单例子以供参考。
---- 步聚
1. 创建一个普通Application。
2. 在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。
3. 连接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;
---- 说明
---- 本程序在Win98+Delphi 5+Excel 97下运行通过。本例子还可以作适当的扩充,如DDE、执行宏调用、保存文件、打印文件及对Excel的设置等,此设置方法请参阅Microsoft Excel Visual Basic参考中的Microsoft Excel对象。

 
about Delphi 5.0中如何读取Excel 97文件

procedure TOfficeform.Button2Click(Sender: TObject);
var MSExcel:Variant;
i,j:Integer;
begin
SaveDialog1.Filter:='*.XLS¦*.XLS';
SaveDialog1.DefaultExt:='XLS';
if SaveDialog1.Execute then
begin
MSExcel:=CreateOLEObject('Excel.Application');
MSExcel.WorkBooks.Add;
MSExcel.Visible:=False;
Table1.Open;
j:=Table1.RecordCount;
Table1.First;
for i:=1 to j do
begin
MSExcel.Cells[i,1].NumberFormat:='@';
MSExcel.Cells[i,1].Value:=Table1.FieldByName('CODE').AsString;
MSExcel.Cells[i,2].Value:=Table1.FieldByName('COLOR').AsString;
Table1.Next;
end;
MSExcel.ActiveWorkBook.SaveAs(SaveDialog1.FileName);
MSExcel.ActiveWorkBook.Saved:=True;
MSExcel.Quit;
end;
end;


利用Delphi 5中调用Excel 97
---- 在Delphi 5中简单地封装了一组Microsoft Office自动化对象(Automation servers)。它使得我们很容易地把Office中的应用程序(Word, Excel, PowerPoint, Outlook and Access等)当作一个com应用服务器进行控制。在Delphi 5中已经带了Word与PowerPoint的例子,因为Excel的调用与这两个应用服务器的调用略有不同,所以本人根据这两个例子写了个Excel 97的简单例子以供参考。
---- 步聚
1. 创建一个普通Application。
2. 在Form中分别放入ExcelApplication, ExcelWorkbook和ExcelWorksheet。
3. 连接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;
---- 说明
---- 本程序在Win98+Delphi 5+Excel 97下运行通过。本例子还可以作适当的扩充,如DDE、执行宏调用、保存文件、打印文件及对Excel的设置等,此设置方法请参阅Microsoft Excel Visual Basic参考中的Microsoft Excel对象。

 
可以调用宏解决
 
多人接受答案了。
 

Similar threads

后退
顶部