var
  fname:string;
  i:integer;
  myexcel,xlBook,xlSheet,xlwork: Variant;
begin
  try
    myexcel:=createoleobject('excel.application');
    xlBook := MyExcel.Application;
  Except
    showmessage('没有安装Excel');
    Exit;
  End;  if TSopendialog.Execute then
    fname:=TSopendialog.FileName;
    xlBook.workBooks.Open(Fname);
      xlBook.Visible := False;
      MyExcel.Visible := true;
      xlSheet := xlBook.ActiveSheet;
      showmessage('ok');
       Screen.Cursor := crHourGlass;
       for i:=2 to xlSheet.UsedRange.Rows.count do
       showmessage('not');
        begin
          DMod.qry_excel.Close;
          DMod.qry_excel.SQL.Clear;
          DMod.qry_excel.SQL.Add('insert into tb_product(name,desc,price)');
          DMod.qry_excel.SQL.Add(' values( inttostr(xlSheet.Cells.Item[i,2]),inttostr(xlsheet.cells.item.[i,3]),inttostr(xlsheet.cell.item.[i,4])');  
          DMod.qry_excel.Open;
        end;
        Screen.Cursor := crDefault;end;
在for i:=2 to xlSheet.UsedRange.Rows.count do出错:
ole error 800A01A8

解决方案 »

  1.   

    try
        xlapp := createoleobject('excel.application');
      except
        showmessage('无法打开Xls文件,请确认已经安装EXCEL!');
        exit;
      end;
      filename := EdtPath.Text;
      xlapp.WorkBooks.Open(filename);
      Sheet :=xlapp.workbooks[1].worksheets[1];
      
      for i:=2 to sheet.usedRange.Cells.rows.count do
      begin    with DMData.QryDataSelect do
        begin
           Close;
           SQL.Clear;
           SQL.Add('insert into newnodes(name,info,directory,ind,parentind) ');
           SQL.Add(' values(:name,:info,:directory,:ind,:parentind)');
           Parameters.ParamByName('name').Value :=sheet.cells[i,2].value;
           Parameters.ParamByName('info').Value :=sheet.cells[i,3].value ;
           Parameters.ParamByName('directory').Value :=sheet.cells[i,4].value;
           Parameters.ParamByName('ind').Value :=sheet.cells[i,5].value;
           Parameters.ParamByName('parentind').Value :=sheet.cells[i,6].value;
           try
             ExecSQL;
           except
           end;
        end;
      end;
      xlapp:=unassigned;