仅供参考:
procedure TForm1.Button1Click(Sender: TObject);
var
i, j,k: integer;
begin
try
ExcelApplication1.Connect;
except
MessageDlg('Excel may not be installed',
mtError, [mbOk], 0);
Abort;
end;
ExcelApplication1.Visible[0] := True;
ExcelApplication1.Caption := 'Excel Application';
ExcelApplication1.Workbooks.Add(Null, 0);
ExcelWorkbook1.ConnectTo
(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo
(ExcelWorkbook1.Worksheets[1] as _Worksheet);
Table1.Open;
for i := 1 to Table1.FieldCount do
begin
for j:=1 to ExcelWorksheet1.usedRange.columns.count do
if ExcelWorksheet1.Cells.Item[1, j]=Table1.Fields[i-1].FieldName then
for k:=2 to ExcelWorksheet1.usedRange.rows.count do begin
if i=1 then
Table1.append
else
begin
Table1.edit;
if k=2 then Table1.first
else Table1.next;
end;
Table1.fields[i - 1].AsString:=ExcelWorksheet1.Cells.Item[k, j];
end;
end;
end;
Table1.post;
Table1.close;
end;