Delphi Excel匯入 的通用程式

來源:互聯網
上載者:User

標籤:操作   部分   hid   progress   const   get   pen   code   int   

 

Delphi Excel匯入 的通用程式 .分類: delphi 2012-09-24 18:19 127人閱讀 評論(0) 收藏 舉報 exceldelphiintegerprocedure 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;beginif (trim(edit1.Text) = ‘‘) then  begin  MessageBox(GetActiveWindow(), 請選擇正確的excel路徑‘, MB_OK +  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(), ‘資料匯入成功‘, ‘提示資訊‘, MB_OK +  MB_ICONWARNING);end;

 

Delphi Excel匯入 的通用程式 2

Delphi Excel匯入 的通用程式 .分類: delphi 2012-09-24 18:20 257人閱讀 評論(0) 收藏 舉報 exceldelphiinteger資料庫c步驟:1 連excel(自己知道其格式,最好是沒個欄位在資料一一對應)2 讀excel資料,填入到資料庫我這裡有個函數,實現把excel表格中資料匯入資料庫,在一條資料匯入前判斷資料庫中是否有該資料,如果有,就不再匯入該資料(避免重複),你可以參考下procedure TForm_qyxxcx.BitBtn2Click(Sender: TObject);VAR  I,J:INTEGER;  col,row:integer;  MsExcel,WBook,WSheet:OLEVARIANT;  f_temp,strtemp:string;begin  if Main_form.lwt.Message_Confirm(‘為預防不可預測情況發生(欄位太長、類型不一致等)‘+#10#13+‘強烈建議在匯入前備份原來資料,‘+#10#13+‘確認:終止匯入‘+#10#13+‘取消:繼續匯入‘) then    abort;  cdsDJZY.Open;  cdsDJZY.First;  while not cdsDJZY.Eof do    cdsDJZY.Delete;  cdsDJZY.Close;  cdsDJZY.Open;  try  begin    MsExcel:= CreateOleObject(‘Excel.Application‘);    WBook:=MsExcel.Application;    if opendialog1.Execute then   //關聯到檔案    begin     if opendialog1.FileName=‘‘ then       abort;      wbook.workbooks.Open(opendialog1.FileName);    end;    WBook.Visible:= true;    WSheet:=WBook.worksheets[1];  end  except  begin    Application.Messagebox(‘您取消了操作或 Excel   沒有安裝!‘,‘ERROR!‘,   MB_ICONERROR   +   mb_Ok);    Abort;  end;  end;  row:=WSheet.UsedRange.Rows.Count;      //行  col:=WSheet.UsedRange.columns.Count;     //列  if (row=0) or (col = 0)  then  begin    showmessage(‘該excel檔案沒有資料!請確認‘);    abort;  end;  proform.Show;  proform.ProgressBar1.Max:=row;  with qqyb do  begin    open;    for i:=1 to row-1 do    //要增加的行數    begin   // 0 人員名稱  2 企業名稱      //判斷是否存在該企業和人員,如果存在,不匯入該記錄      strtemp:=‘select * from qyb where qy_name = ‘‘‘+WSheet.cells[i+1,1].Value+‘‘‘‘;      Main_Form.lwt.DB_AdoQueryRun(qupdate,strtemp);      if qupdate.RecordCount<>0 then   //  存在,加入暫存資料表  ,退出本次迴圈      begin        cdsDJZY.Append ;        cdsDJZY.FieldByName(‘企業名稱‘).AsString := WSheet.cells[i+1,1].Value;        cdsDJZY.Post;        continue;      end;      Append; //不存在,繼續      for j:=0 to col-1 do   //列      begin        proform.ProgressBar1.Position:=proform.ProgressBar1.Position+1;//        if adory.Fields[j].FieldKind        Fields[j].Value:= WSheet.cells[i+1,j+1].Value;      end;      Post;    end;  end;  proform.ProgressBar1.Position:=0;  proform.Hide;  if cdsDJZY.RecordCount>1 then  begin    if Main_form.lwt.Message_Confirm(‘資料已經匯入!,部分記錄因為已經存在,沒有匯入,是否列印沒有匯入記錄的請單?‘) then    begin      Main_form.lwt.DB_ShowReportByDataSet(cdsDJZY,‘因為已經存在該企業匯入失敗的資訊‘);      main_form.lwt.DB_Excel_Export(cdsdjzy,‘c:\hello.xls‘);    end;  end  else  begin    Main_Form.lwt.Message_Show(‘資料已經匯入!‘);  end;  wbook.workbooks.close;end;

 

Delphi Excel匯入 的通用程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.