uses ComObj;
try
ExcelApp := CreateOleObject( 'Excel.Application' );
ExcelApp.visible:=False;
ExcelApp.Workbooks.Open(Trim(edt1.Text));//创建COMOBJ对象,并且打开指定文件
except
ShowMessage('打开Excel文件出错,请检查文件是否损坏 或者 系统是否安装了 Microsoft Excel!');
exit;
end;
try
ADOConnection1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + Trim(edt1.Text) + ';Extended Properties=Excel 8.0';//设置ADO连接参数,此时并未打开连接
sheetname:= TStringList.Create;
for i:=1 to ExcelApp.WorkBooks[1].Worksheets.Count do //一个EXCEL有多页情况,必须循环多次来查询
sheetname.Add(ExcelApp.WorkBooks[1].Worksheets[i].Name);
finally
ExcelApp.WorkBooks.Close;
ExcelApp.Quit;
ExcelApp:=Unassigned;
end;
//这一段其实就是为了得到指定EXCEL文件的各个分页名,而且必须在ADO连接前释放COMOBJ对象,因为ExcelApp正在打开 指定文件(独占方式),,ADO等会也会打开会产生冲突
try
ERRcount:=0;
SUCcount:=0;
for i:=0 to sheetname.count-1 do
begin
try
adoquery1.close;
adoquery1.SQL.Text := 'Select distinct 列名1 from [' + SheetName[i] + '$]';/页名右边
adoquery1.Open;
if adoquery1.RecordCount > 0 then
begin
pb2.Max := adoquery1.RecordCount;
pb2.Position :=0;
adoquery1.First;
adoquery1.DisableControls;
while not adoquery1.Eof do
begin
mobile := trim(adoquery1.fields[0].asstring);//此示例中仅为了获取第一列(列名1)
mbtype := GetMobileType(addphonestr(mobile));
if mbtype <> -1 then
begin
new(psendData);
psendData^.ssubItems := nil;
psendData^.sCaption := addphonestr(mobile);
psendData^.mbtype := 1 + mbtype; //-1错号,01小灵通,23手机
sendfilelist.Add(psendData);
end;
adoquery1.Next;
end;
end;
SUCcount:=SUCcount+1;//纪录成功加载的页数
except
ERRcount:=ERRcount+1;//纪录加载失败的页数
end;
end;
finally
sheetname.Free;
ADOConnection1.Close;
end;