用OLE实现,可以参考一下下面的代码:procedure TForm1.importBtnClick(Sender: TObject); var I,J,k,Row,Col,sRow,sCol: integer; ExcelFile: string; TmpString: String; begin ExcelFile := Trim(Edit1.Text); Row := strtoint(Edit4.Text); //the total Rows Col := strtoint(Edit5.Text); //the total Columns sRow := strtoint(Edit2.Text); //the start of Row sCol := strtoint(Edit3.Text); //the start of Column try if VarIsEmpty(XlsApp) then XlsApp := CreateOleObject('Excel.Application'); XlsSheet := XlsApp.workbooks.open(ExcelFile); for I := sRow to (sRow + Row) - 1 do //from the start row to the end row begin k := 0; //the item of the field TmpString := ''; Table1.open; Table1.Append; for J := sCol to (sCol + Col) - 1 do //from the start column to the end column begin TmpString := XlsSheet.ActiveSheet.Cells[I,J].Text; Table1.Fields[k].AsString := TmpString; k := k + 1; end; Table1.Post; end; XlsApp.Visible := true; except XlsSheet.close; XlsApp.Application.quit; XlsApp := Unassigned; XlsSheet := Unassigned; end; end;
var
I,J,k,Row,Col,sRow,sCol: integer;
ExcelFile: string;
TmpString: String;
begin
ExcelFile := Trim(Edit1.Text);
Row := strtoint(Edit4.Text); //the total Rows
Col := strtoint(Edit5.Text); //the total Columns
sRow := strtoint(Edit2.Text); //the start of Row
sCol := strtoint(Edit3.Text); //the start of Column
try
if VarIsEmpty(XlsApp) then
XlsApp := CreateOleObject('Excel.Application');
XlsSheet := XlsApp.workbooks.open(ExcelFile);
for I := sRow to (sRow + Row) - 1 do //from the start row to the end row
begin
k := 0; //the item of the field
TmpString := '';
Table1.open;
Table1.Append;
for J := sCol to (sCol + Col) - 1 do //from the start column to the end column
begin
TmpString := XlsSheet.ActiveSheet.Cells[I,J].Text;
Table1.Fields[k].AsString := TmpString;
k := k + 1;
end;
Table1.Post;
end;
XlsApp.Visible := true;
except
XlsSheet.close;
XlsApp.Application.quit;
XlsApp := Unassigned;
XlsSheet := Unassigned;
end;
end;
XlsApp,XlsSheet: Variant;