用 OLE 读取 EXCEL 文档。
procedure TForm1.Button1Click(Sender: TObject);
var ExcelApp,MyWorkBook: OLEVariant;
i,j,n: Integer;
totalnum : real;
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',
mb_OK+mb_IconStop);
Exit;
end;
totalnum := 0;
n := 总行数
MyworkBook:= ExcelApp.workBooks.Open(ExtractFileDir(Application.ExeName)
+ '/' + Edit1.Text + '.xls');
for i := 0 to n-1 do //n代表行数
begin
totalnum := totalnum+MyWorkBook.WorkSheets[1].Cells[2,i].Value;
end;
MyWorkBook.WorkSheets[1].Cells[3,n].Value := totalnum;
ExcelApp:=Unassigned; //释放VARIANT变量
end;