在Delphi中用ADO连接Excel,把Excel中的数据导到DBGridEh
如果Excel某个单元格的格式有错误——注意:是Excel判断得出的格式错误,不是误把文本格式设成了数字格式的那种错误,数据就读取不过来这个问题怎么解决啊?

解决方案 »

  1.   

     使用OLE对象打开每个CELL,读取  try
        if ExcelApp = nil then
        begin
          ExcelApp := TExcelApplication.Create(Owner);
        end;
        if Workbook = nil then
        begin
          Workbook := TExcelWorkbook.Create(Owner);
          Workbook.ConnectTo(
            ExcelApp.Workbooks.Add(NULL, 0));
        end;
        if WorkSheet = nil then
        begin
          Worksheet := TExcelWorksheet.Create(Owner);
          Worksheet.ConnectTo(Workbook.Worksheets.Item[1] as _Worksheet);
        end;
        Result := True;
      except
        Result := False;
        FreeAndNil(ExcelApp);
        FreeAndNil(WorkBook);
        FreeAndNil(WorkSheet);
        Msg := '无法创建Excel,也许没有安装Excel!';
        MessageDlg(Msg, mtError, [mbOk], 0);
        raise Exception.Create(Msg +
          #13'请安装Excel,或到安装了Excel的工作站上在执行此操作!');
        Exit;
      end;