Delphi excel 데이터베이스로 가져오기

uses ComObj;
 
procedure TMemo_form_form.N5Click(Sender: TObject); var   ExcelApp: Variant;//EXCEL에서 객체 link:string을 엽니다.  i:integer; begin   if dlgOpen1.Execute then   begin    link:=dlgOpen1.FileName;        try         ExcelApp:= CreateOleObject('Excel.Application');        ExcelApp.WorkBooks.Open(link); {EXCEL 파일을 엽니다. 그렇지 않으면 외부 테이블이 예상한 형식이 아니라는 것을 알립니다} con1.Connected:=False ;        qry1.Active :=False ;
        //con1:ADOConnection           con1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Password="";DataSource=';        con1.ConnectionString:=con1.ConnectionString+link;        con1.ConnectionString:=con1.ConnectionString+';ExtendedProperties=excel 8.0;Persist Security Info=false';        con1.LoginPrompt:=False ;        con1.Connected:=True;
        //qry1:ADOQuery          qry1.close;        qry1.SQL.Clear;        qry1.sql.Add('SELECT * FROM [sjdr$]');        qry1.open;
       if Qry1.RecordCount > 0 then        begin         for i:=1 to qry1.RecordCount  do         begin           Application.ProcessMessages;           self.refresh;           ClientDataSet5.Close;           ClientDataSet5.CommandText:='insert into czxdb(IDBH,NUM,CZXDNR,CZTIME,CZXDBT,CZBZMC)values(:a,:b,:c,:d,:e,:f)';           ClientDataSet5.Params[0].AsString :=qry1.FieldByName('IDBH').AsString;           ClientDataSet5.Params[1].AsString :=qry1.FieldByName('NUM').AsString;           ClientDataSet5.Params[2].AsString :=qry1.FieldByName('CZXDNR').AsString;           ClientDataSet5.Params[3].AsString :=qry1.FieldByName('CZTIME').AsString;           ClientDataSet5.Params[4].AsString :=qry1.FieldByName('CZXDBT').AsString;           ClientDataSet5.Params[5].AsString :=qry1.FieldByName('CZBZMC').AsString;
          try             ClientDataSet5.Execute;           except           end;           Application.ProcessMessages;           Qry1.Next;          end;        end;
       try          ClientDataSet5.Execute; ShowMessage(가져오기 성공);Except ShowMessage(가져오기 실패);       end; {EXCEL 파일 캐시, 파일 닫기, 종료}ExcelApp.ActiveWorkBook.Saved := True;
{EXCEL 파일 캐시, 파일 닫기, 종료} Finally ExcelApp.WorkBooks.Close;        ExcelApp.Quit;      end;       end;      end;
//excel    dbgrid

var
  str:string;
begin
  str:='select * from opendatasource(''microsoft.jet.oledb.4.0'',''data source="    ";user id=admin;password=;extened properties=excel 5.0'')...[sheet1$]';
  adoquery.close;
  adoquery.sql.clear;
  adoquery.sql.add(str);
  adoquery.active:=true;
  adoquery.open;
end;

//dbgrid      

var
  str:string;
begin
  str:='select *  into t from opendatasource(''microsoft.jet.oledb.4.0'',''data source="    ";user id=admin;password=;extened properties=excel 5.0'')...[sheet1$]';
  with adoquery do
    begin
      close;
      sql.clear;
      sql.add(str);
      execsql;
      if rowsaffected>0 then
        showmessage('    ')
      else
        showmessage(    );
    end;
   
end;

procedure TForm1.btnClick(Sender: TObject); begin   OpenDialog1.Title: ='올바른 excel 파일을 선택하십시오'.  OpenDialog1.Filter := 'Excel(*.xls)|*.xls';   if OpenDialog1.Execute then   edit1.Text := OpenDialog1.FileName; end; procedure TForm1.btninClick(Sender: TObject); const       BeginRow = 2; BeginCol = 1; var   Excel: OleVariant;   iRow,iCol : integer;   xlsFilename: string; begin if(trim (edit 1.Text) =') then begin MessageBox (GetActive Window (), 정확한 excel 경로를 선택하십시오', MBOK +   MB_ICONWARNING);   exit;   end;   xlsFilename := trim(edit1.Text);   try   Excel := CreateOLEObject('Excel.Application');   except   Application.MessageBox('excel에 설치되지 않음','알림 정보', MB OK+MB ICONASTERISK+MB DEFBUTTON1+MB APPLMODAL);  Exit;   end;   Excel.Visible := false;   Excel.WorkBooks.Open(xlsFilename);   try   iRow := BeginRow;   iCol := BeginCol;       while trim(Excel.WorkSheets[1].Cells[iRow,iCol].value) <> '' do begin   with ADOQuery1 do begin   Append;   Fields[0].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol].value);   Fields[1].AsString := trim(Excel.WorkSheets[1].Cells[iRow,iCol+1].value);   Fields[2].Asstring := trim(Excel.WorkSheets[1].Cells[iRow,iCol+2].value);   iRow := iRow + 1;   end;       end;   Excel.Quit;   ADOQuery1.UpdateStatus ;   except   Application.MessageBox('데이터 가져오기 오류','힌트 정보', MB OK+MB ICONASTERISK+MB DEFBUTTON1+MB APPLMODAL)  Excel.Quit;   end; MessageBox(GetActiveWindow(), 데이터 가져오기 성공, 프롬프트, MBOK +   MB_ICONWARNING); end;

좋은 웹페이지 즐겨찾기