参照一下吧,我的方法是直接用 OLE 读取 EXCEL 文档。
procedure TForm1.Button1Click(Sender: TObject);
var ExcelApp,MyWorkBook: OLEVariant;
i,j: Integer;
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
MyWorkBook:=CreateOleobject('Excel.Sheet');
except
application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',
mb_OK+mb_IconStop);
Exit;
end;
//ExcelApp.Visible := true;
MyworkBook:= ExcelApp.workBooks.Open(ExtractFileDir(Application.ExeName)
+ '/' + Edit1.Text + '.xls');
for i := 3 to 9 do begin
//Read a row into table
table1.Append;
table1.FieldByName('Name').AsString := MyWorkBook.WorkSheets[1].Cells[i,1].Value;
table1.FieldByName('Size').AsString := MyWorkBook.WorkSheets[1].Cells[i,2].Value;
table1.FieldByName('Weight').AsString := MyWorkBook.WorkSheets[1].Cells[i,3].Value;
table1.FieldByName('Area').AsString := MyWorkBook.WorkSheets[1].Cells[i,4].Value;
end;
//showmessage(MyWorkBook.WorkSheets[1].Columns[1].numberformat);
//showmessage(MyWorkBook.WorkSheets[1].Columns[2].numberformat);
ExcelApp:=Unassigned; //释放VARIANT变量
end;