如题,我看到百度文库里有相关的代码,可是粘贴到delpi中好多都是错的 比如说
ExcelApp,WorkBook: Olevariant;
ExcelApp:=CreateOleObject('Excel.Application');creatoleobject这里就是红色波浪线。。显示是错的
有没有高手给我可以实现这个功能的完整代码呀~~~~~~

解决方案 »

  1.   

    CreateOleObject处报错,是因为没有引用ComObj单元
      

  2.   


    可以了~~thx~~ ExcelApp,WorkBook:Olevariant;    ExcelRowCount:=WorkBook.WorkSheets[1].UsedRange.Rows.Count;
        for i := 1 to excelrowcount + 1 do
        begin
          excelx := excelapp.Cells[i,1].Value;
          excely := excelapp.Cells[i,2].Value;
    这里呢 是不是也是因为我没有引用啊?
      

  3.   


    可以了~~thx~~ ExcelApp,WorkBook:Olevariant;    ExcelRowCount:=WorkBook.WorkSheets[1].UsedRange.Rows.Count;
        for i := 1 to excelrowcount + 1 do
        begin
          excelx := excelapp.Cells[i,1].Value;
          excely := excelapp.Cells[i,2].Value;
    这里呢 是不是也是因为我没有引用啊?
    引用:ComObj
    下面是导入代码:
    var
        ASql, APath: String;
        ExcelApp: Variant;
        AInt: Integer;
    begin
        with ADOQuery2 do
        begin
            Close;
            sql.Clear;
            SQL.Add('select MAX(ordno) as SDREN from ordtelf');
            Open;
            if FieldByName('SDREN').AsString<>'' then
            begin
             MessageBox(Self.Handle,'你数据库有未完成的导入的数据,请等待之前的数据完成在导入!','系统提示',mb_iconquestion);
            end
            else
            begin                  if messagebox(Self.Handle,'请选择业务配送数据包!','系统提示',mb_yesno+mb_iconquestion)=idyes then
                          begin
                              If OpenDialog1.Execute Then
                                APath := OpenDialog1.FileName
                              Else
                                Exit;                          If not FileExists(APath) Then
                                Begin
                                  messagebox(self.Handle,'你选择的文件不存,请核对','提示信息',mb_iconinformation);
                                  Exit;
                                End;
                              Try
                                Application.ProcessMessages;
                                ExcelApp := CreateOleObject('Excel.Application');
                                ExcelApp.Workbooks.Open(APath);
                                ExcelApp.WorkSheets[1].Activate;                            ASql := 'select * from lf where 1=2';     //表名 ,也可以在show里面先打开数据集
                                 ADOQuery1.Close;       //数据集的 LockType属性 要设为 ltBatchOptimistic
                                 ADOQuery1.SQL.Text := ASql;    //才可以批量保存
                                 ADOQuery1.Open;
                                For AInt := 2 To ExcelApp.ActiveSheet.UsedRange.rows.count Do //AInt 起始行数,重哪行开始导入,Aint初始值就是几
                                  Begin
                                         ADOQuery1.Append;
                                         ADOQuery1.FieldByName('ordno').AsString := ExcelApp.Cells[AInt, 1].Value;   //Aint是第几行,后面的数字是第几列,
                                         ADOQuery1.FieldByName('ordtype').AsString := ExcelApp.Cells[AInt, 2].Value;
                                         ADOQuery1.FieldByName('ordbdate').AsDateTime := ExcelApp.Cells[AInt, 3].Value;
                                         ADOQuery1.FieldByName('ordedate').AsDateTime := ExcelApp.Cells[AInt, 4].Value;
                                         ADOQuery1.FieldByName('ordsd').AsString := ExcelApp.Cells[AInt, 5].Value;
                                         ADOQuery1.FieldByName('ordmd').AsString := ExcelApp.Cells[AInt, 6].Value;
                                         ADOQuery1.FieldByName('ordcity').AsString := ExcelApp.Cells[AInt, 7].Value;
                                         ADOQuery1.FieldByName('ordcon').AsString := ExcelApp.Cells[AInt, 8].Value;
                                         ADOQuery1.FieldByName('ordadd').AsString := ExcelApp.Cells[AInt, 9].Value;
                                         ADOQuery1.FieldByName('ordtel').AsString := ExcelApp.Cells[AInt, 10].Value;
                                         ADOQuery1.FieldByName('ordmey').AsString := ExcelApp.Cells[AInt, 11].Value;
                                         ADOQuery1.FieldByName('ordsku').AsString := ExcelApp.Cells[AInt, 12].Value;
                                         ADOQuery1.FieldByName('ordskuname').AsString := ExcelApp.Cells[AInt, 13].Value;
                                         ADOQuery1.FieldByName('ordqty').AsFloat := ExcelApp.Cells[AInt, 14].Value;
                                         ADOQuery1.FieldByName('ordte').AsString := ExcelApp.Cells[AInt, 15].Value;
                                         ADOQuery1.Post;
                                       End;
                                 Finally
                                   ExcelApp.WorkBooks.Close;
                                   ExcelApp.quit;
                                 End;
                          end;        end;
        end;
      

  4.   


    系统可以跑起来 执行导入订单功能是就不行
    First chance exception at $7581812F. Exception class EOleSysError with message '没有注册类, ProgID: "Excel.Application"'. Process Project1.exe (9216)First chance exception at $7581812F. Exception class EVariantInvalidOpError with message 'Invalid variant operation'. Process Project1.exe (9216)
      

  5.   

    上面断点处的代码是这一句
    ExcelID := CreateOleObject( 'Excel.Application' );
      

  6.   

    procedure DBGrid2Excel(DBGrid:TDBGrid;ExcelFileName:string);
    var  MyExcel: Variant;
         x,y:integer;
    begin
      deletefile(ExcelFileName);
      MyExcel := CreateOleOBject('Excel.Application');  MyExcel.WorkBooks.Add;
      MyExcel.Visible := True;
      MyExcel.WorkBooks[1].Saveas(ExcelFileName);
      dbgrid.DataSource.DataSet.First;  y:=1;
      for x:=1 to dbgrid.FieldCount do
      begin
        MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,x] := dbgrid.Fields[x-1].DisplayName;
        MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,x].Select;
        MyExcel.Selection.Font.Bold := true;
        MyExcel.WorkBooks[1].WorkSheets[1].Columns[x].ColumnWidth := dbgrid.Fields[x-1].DisplayWidth;
      end;
      inc(y);
      while not dbgrid.DataSource.DataSet.eof do
      begin
        for x:=1 to dbgrid.FieldCount do
        begin
           MyExcel.WorkBooks[1].WorkSheets[1].Cells[y,x] := dbgrid.Fields[x-1].AsString;
        end;
        inc(y);
        dbgrid.DataSource.DataSet.next;
      end;
    end;
      

  7.   

    这台电脑有装Excel吗
      

  8.   


    有哇。。
    你确定装的是微软的excel?不是WPS?