我有一个excel表格,文件名为yy.xls,里面有一个表单,为sheet1,
我想将表单sheet1的内容导入orcale数据库aa表中,
请问如何实现?
请提供源码!

解决方案 »

  1.   

    myexcel:olevariant;
      workbook:olevariant;
      worksheet:olevariant;
      i,j:integer;
      begin
      if  length(form1.FlatEdit1.Text)<>8 then begin
          showmessage('程序发生异常,你可能没有输入期或你输入的日期位数不够!');
          end else begin
      try
       myexcel:=CreateOLEObject('Excel.application') ;
       myexcel.application.workbooks.add;
       myexcel.caption:='由数据库数据倒入EXCEL表中';
       myexcel.application.visible:=true;
       workbook:=myexcel.application.workbooks[1];
       worksheet:=workbook.worksheets.item[1];
       except
        showmessage('你还没有安装EXCEL!!!');
       end;
       i:=0;
      table1.DatabaseName:='c:\cxbb';
      table1.TableName:='cxdb.db';
      screen.Cursor:=crHourGlass;
      table1.Active:=true;
      table1.first;
      while not table1.eof do
       begin
        inc(i);
        for j:=0 to table1.fieldcount-1 do
          worksheet.cells[i,j+1]:=table1.fields[j].asstring;
          table1.next;
       end;
       worksheet.Rows[1].insert;
       worksheet.cells[1,1]:='11';
       worksheet.cells[1,2]:=' 22';
       worksheet.cells[1,3]:='33';
       worksheet.cells[1,4]:='44‘;
       worksheet.cells[1,5]:='55’;
       worksheet.cells[1,6]:='66‘;
       worksheet.cells[1,7]:='77’;
       worksheet.Rows[2].insert;
       worksheet.cells[2,1]:='xx';
       worksheet.cells[2,2]:=' 22';
       worksheet.cells[2,3]:='时间2002/01/01';
       worksheet.cells[2,4]:=inttostr(strtoint(flatedit1.Text)-1);
       worksheet.cells[2,5]:=flatedit1.Text;
       worksheet.cells[2,6]:='       ';
       worksheet.cells[2,7]:='       ';
       worksheet.Pagesetup.centerHeader:='报表';
       worksheet.Columns[1].ColumnWidth := 20;
       worksheet.Columns[2].ColumnWidth := 8;
       worksheet.Columns[3].ColumnWidth := 14;
       worksheet.Columns[4].ColumnWidth := 14;
       worksheet.Columns[5].ColumnWidth := 14;
       worksheet.Columns[6].ColumnWidth := 14;
       worksheet.Columns[7].ColumnWidth := 14;
       worksheet.Range[ 'A1:G58' ].Borders[3].Weight := 2;
       worksheet.Range[ 'A1:G58' ].Borders[1].Weight := 2;
       worksheet.Range[ 'G1:G58' ].Borders[2].Weight := 2;
       worksheet.Range[ 'A58:G58' ].Borders[4].Weight := 2;
       table1.Close;
    //   myexcel.Save( 'C:\cxbb\cxbb.xls' );
    //   myexcel.WorkBooks.Close;
    //   myexcel.quit;
       screen.Cursor:=crdefault;
       end;
    --------------------------------------------------
    但愿上面的代码对你有所帮助,你用ADO连接!!!
      

  2.   

    楼上的,
    谢谢你,
    不过我想要实现的是从excel表中导入数据库中哦!~
      

  3.   

    procedure TNewCardForm.Button1Click(Sender: TObject);
    var
        ExcelApp,MyWorkBook: OLEVariant;
        i,j,hykhlen,k,m,num: Integer;
        tmpName,
        tmpstr,
        newcardstr,
        tmphykh:String;begin
      num:=0;
      if opendialog1.Execute then
      begin
        try
          ExcelApp:=CreateOleObject('Excel.Application');
          MyWorkBook:=CreateOleobject('Excel.Sheet');
        except
          application.Messagebox('无法打开Xls文件,请确认已 经安装EXCEL.','',
               mb_OK+mb_IconStop);
          Exit;
        end;
        MyworkBook:= ExcelApp.workBooks.Open(opendialog1.FileName);
        MyworkBook.Protect('',true);    //打开文件后,对文件进行操作
        for i:=2 to {11} 1100 do
        begin
          j:= MyWorkBook.WorkSheets[1].Cells[i,1].Value;
          try
            tmphykh:=inttostr(j);
          except
            showmessage('can not convert to string, error at the'+inttostr(i)+'line');
            exit;
          end;
          tmpName:= MyWorkBook.WorkSheets[1].Cells[i,2].Value;
          hykhlen:= Length(tmphykh);
          newcardstr:=  tmpHykh;
          if hykhlen<>0 then
          begin
            if hykhlen  <5 then
            begin
              k:=5-hykhlen;
              tmpstr:='';
              for m:=0 to k-1 do
                  tmpstr:=tmpstr+'0';
              newcardstr:=tmpstr+tmpHykh;
            end
            else if hykhlen  >5 then
            begin
              k:=hykhlen-5;
              newcardstr:=copy(tmphykh,k,5);
            end;
            //showmessage(newcardstr);
            with adoquery2 do
            begin
              close;
              SQL.Clear;
              SQL.Add('select id,hykh,[name] from customer where hykh=:hykhvalue');
              Parameters.ParamByName('hykhvalue').Value:= '00'+newcardstr;
              Open;
              prior;
              if not eof then
                if trim(tmpName)  = trim(FieldByName('Name').AsString) then
                begin
                  close;
                  SQL.Clear;
                  SQL.Add('update customer set newcard='''+newcardstr+''' where hykh=:hykhvalue');
                  Parameters.ParamByName('hykhvalue').Value:= '00'+newcardstr;
                  ExecSQL;
                  num:=num+1;
                end;
            end;
          end;
        end;
      end;
      showmessage('你一共更新了'+InttoStr(num)+'行数据!');
      ADOQuery1.Close;
      ADOQuery1.Open;
      ExcelApp.quit;
      ExcelApp:=Unassigned; //释放VARIANT变量
    end;把EXCEL文件做为ole读入到程序中,ORACLE数据库的连接用ado来连接
      

  4.   

    to  Drate(鸟窝里的虫) 
    程序运行时,提示CreateOleObject没有定义!
      

  5.   

    to  Drate(鸟窝里的虫) 
    程序运行时,提示Parameters没有定义!