1.遍历目录
//判斷是不是目錄
function IsValidDir(SearchRec: TSearchRec): Boolean;
begin
result:= (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name <> '.') and (SearchRec.Name <> '..');
end;
//傳入一個路徑,查找這個路徑下的所有文件(也查找所有子目錄下的所有文件)
procedure SearchAllPath(Path: string);
var
i: Integer;
sl_SubDir: TStrings;
SearchRec: TSearchRec;
begin
CheckStop;
if Trim(Path) = '' then exit;
sl_SubDir:= TStringList.Create;
if (FindFirst(Path + '/*.*', faDirectory, SearchRec) = 0) then
begin
if IsValidDir(SearchRec) and cbSubDir.Checked then sl_SubDir.Add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) and cbSubDir.Checked then sl_SubDir.Add(SearchRec.Name)
else if IsValidFile(SearchRec) then DoFile(Path, SearchRec.Name); //找到文件,進行處理
end;
end;
FindClose(SearchRec);
for i:= 0 to sl_SubDir.Count - 1 do
SearchAllPath(Path + '/' + sl_SubDir.Strings);
sl_SubDir.Free;
end;
2.打开excel文件,读取你需要的数据.
ADOConnection2.Close;
ADOConnection2.ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="DSN=Excel Files;'+
'DBQ='+LMDFileOpenEdit1.FileName+';DriverId=790;MaxBufferSize=2048;PageTimeout=5;"';
ADOConnection2.Open;
ADOConnection2.GetTableNames(ComboBox2.Items,true);
table_name:=ComboBox2.Items.Strings[0];
ExcelQuery1.Close;
ExcelQuery1.SQL.Clear;
// ExcelQuery1.ConnectionString:='Provider=MSDASQL.1;Persist Security Info=False;Extended Properties="DSN=Excel Files;'+
// 'DBQ='+lmdfileopenedit1.Filename+';DriverId=790;MaxBufferSize=2048;PageTimeout=5;"';
ExcelQuery1.SQL.Add('select * from ['+table_name+']');
ExcelQuery1.Open;