DELPPHI 通过ADO读取EXCEL

 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; 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值