打开一个EXCEL文件,代码:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var WriteExcel:OLEVariant;
i,j:integer;
begin
WriteExcel:=CreateOLEObject('Excel.Application');//这里报错:[Error] Unit1.pas(30): Undeclared identifier: 'CreateOLEObject'
// WriteExcel.WorkBooks.Create('d:\ok.xls');
WriteExcel.WorkBooks.Add();
WriteExcel.Visible:=True;
for i:=1 to 3 do
for j:=1 to 5 do
begin
WriteExcel.Cells[i,j].Value:='I='+IntToStr(i)+'J='+IntToStr(j);
end;
WriteExcel.ActiveWorkBook.Save;
WriteExcel.ActiveWorkBook.Close;
WriteExcel.Quit;
end;end.我应该怎么identifier CreateOLEObject呢?
其他打开EXCEL的例子我都在这里看过了,没有我会用的,希望哪位大大能给个实例下载最好,感激不尽!!!!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var WriteExcel:OLEVariant;
i,j:integer;
begin
WriteExcel:=CreateOLEObject('Excel.Application');//这里报错:[Error] Unit1.pas(30): Undeclared identifier: 'CreateOLEObject'
// WriteExcel.WorkBooks.Create('d:\ok.xls');
WriteExcel.WorkBooks.Add();
WriteExcel.Visible:=True;
for i:=1 to 3 do
for j:=1 to 5 do
begin
WriteExcel.Cells[i,j].Value:='I='+IntToStr(i)+'J='+IntToStr(j);
end;
WriteExcel.ActiveWorkBook.Save;
WriteExcel.ActiveWorkBook.Close;
WriteExcel.Quit;
end;end.我应该怎么identifier CreateOLEObject呢?
其他打开EXCEL的例子我都在这里看过了,没有我会用的,希望哪位大大能给个实例下载最好,感激不尽!!!!
你没调用'CreateOLEObject'这个函数的单元,就好像你类form1想用类Form2的函数,得uses unit2;
uses
comobj;
procedure TForm1.Button1Click(Sender: TObject);
var ea:variant;
begin
try
EA:=CreateOleObject('Excel.Application');
except
application.MessageBox('NO EXCEL',' ', MB_OK);
EXIT;
end;
EA.Visible := true;
application.BringToFront;
EA.WorkBooks.Add; EA.Cells[1,1]:='aa';
EA.Cells[1,2]:='bb'; EA.ActiveWorkBook.Saveas('aaa.xls');
ea.quit;
ea:=unassigned;
end;
comobj;
var
ExcelApp,WorkBook:Variant;
begin
strName := 'C:\'+'My.xls';
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('ÄãµÄ»úÆ÷Àïδ°²×°Microsoft Excel. ','Ìáʾ',MB_OK+MB_ICONINFORMATION);
Exit;
end;
WorkBook := ExcelApp.WorkBooks.Add;
for i:=1 to 3 do
for j:=1 to 5 do
begin
WriteExcel.Cells[i,j].Value:='I='+IntToStr(i)+'J='+IntToStr(j);
end;
WorkBook.SaveAs(strName, 56); // Office Excel 97-2003 format
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned; end;
comobj;
var
ExcelApp,WorkBook:Variant;
i ,j Integer;
strName : String ;
begin
strName := 'C:\'+'My.xls';
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('ÄãµÄ»úÆ÷Àïδ°²×°Microsoft Excel. ','Ìáʾ',MB_OK+MB_ICONINFORMATION);
Exit;
end;
WorkBook := ExcelApp.WorkBooks.Add;
for i:=1 to 3 do
for j:=1 to 5 do
begin
WriteExcel.Cells[i,j].Value:='I='+IntToStr(i)+'J='+IntToStr(j);
end;
WorkBook.SaveAs(strName, 56); // Office Excel 97-2003 format
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned; end;
comobj;
var
ExcelApp,WorkBook:Variant;
i ,j Integer;
strName : String ;
begin
strName := 'C:\'+'My.xls';
try
ExcelApp := CreateOleObject('Excel.Application');
WorkBook := CreateOleObject('Excel.Sheet');
except
Application.MessageBox('ÄãµÄ»úÆ÷Àïδ°²×°Microsoft Excel. ','Ìáʾ',MB_OK+MB_ICONINFORMATION);
Exit;
end;
WorkBook := ExcelApp.WorkBooks.Add;
for i:=1 to 3 do
for j:=1 to 5 do
begin
WorkBook.Cells[i,j].Value:='I='+IntToStr(i)+'J='+IntToStr(j);
end;
WorkBook.SaveAs(strName, 56); // Office Excel 97-2003 format
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned; end;