这里有一段代码,大家帮我找找错,要如何修改?(这段代码肯定出错)(50分)

  • 主题发起人 主题发起人 guguda
  • 开始时间 开始时间
G

guguda

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);
var ExcelApp,MyWorkBook: OLEVariant;
i: Integer;
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('无法打开Xls文件请确认已经安装EXCEL.','',mb_OK+mb_IconStop);
Exit;
end;
if OpenDialog1.Execute then
MyworkBook:= ExcelApp.workBooks.Open(OpenDialog1.FileName);
i:=1;
while trim(string(MyWorkBook.WorkSheets[1].item[i,1]))<>''do
adotable1.Append;
adotable1.FieldByName('Name').AsString := MyWorkBook.WorkSheets[1].Cells[i,1].Value;
adotable1.FieldByName('type').AsString := MyWorkBook.WorkSheets[1].Cells[i,2].Value;
adotable1.FieldByName('area').AsString := MyWorkBook.WorkSheets[1].Cells[i,3].Value;
adotable1.FieldByName('Area_no').AsString := MyWorkBook.WorkSheets[1].Cells[i,4].Value;
adotable1.FieldByName('piece').AsString := MyWorkBook.WorkSheets[1].Cells[i,5].Value;
adotable1.FieldByName('min').AsString := MyWorkBook.WorkSheets[1].Cells[i,6].Value;
adotable1.FieldByName('fee').AsString := MyWorkBook.WorkSheets[1].Cells[i,7].Value;
inc(i);
end;
ExcelApp:=Unassigned;
 
procedure TForm1.Button1Click(Sender: TObject);
var ExcelApp,MyWorkBook: OLEVariant;

i: Integer;
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook:=CreateOleobject('Excel.Sheet');//这里出错,应该是Excel.book//或者不要这句

except
application.Messagebox('无法打开Xls文件请确认已经安装EXCEL.','',mb_OK+mb_IconStop);
Exit;
end;
if OpenDialog1.Execute then
MyworkBook:= ExcelApp.workBooks.Open(OpenDialog1.FileName);
//你要先确定adotable1是否已经打开了
i:=1;
while trim(string(MyWorkBook.WorkSheets[1].item[i,1]))<>''do
adotable1.Append;
adotable1.FieldByName('Name').AsString := MyWorkBook.WorkSheets[1].Cells[i,1].Value;
adotable1.FieldByName('type').AsString := MyWorkBook.WorkSheets[1].Cells[i,2].Value;
adotable1.FieldByName('area').AsString := MyWorkBook.WorkSheets[1].Cells[i,3].Value;
adotable1.FieldByName('Area_no').AsString := MyWorkBook.WorkSheets[1].Cells[i,4].Value;
adotable1.FieldByName('piece').AsString := MyWorkBook.WorkSheets[1].Cells[i,5].Value;
adotable1.FieldByName('min').AsString := MyWorkBook.WorkSheets[1].Cells[i,6].Value;
adotable1.FieldByName('fee').AsString := MyWorkBook.WorkSheets[1].Cells[i,7].Value;
//adotable1应该post一下
inc(i);
end;
//这里要先关闭book

//再关闭Excel

//再用你下面的那句
ExcelApp:=Unassigned;
 
to yanghai0437
先关闭book,再关闭excel的代码是什么?

运行后出现“Method 'item' not supported by automation object”错误。

另外,为什么excel没有被释放?在关机时发现有一个EXCEL还在后台运行。
 
接受答案了.
 
后退
顶部