Generic program for Delphi Excel import

Source: Internet
Author: User
Tags import database

Generic Program for Delphi Excel Import. Category: Delphi -- the- -  -: + 127People reading reviews (0) Collection Report Exceldelphiintegerprocedure Tform1.btnclick (sender:tobject);beginOpendialog1.title:='Please select the correct Excel file'; Opendialog1.filter:='Excel (*.xls) |*.xls'; ifOpendialog1.execute ThenEdit1. Text:=Opendialog1.filename;End;procedureTform1.btninclick (sender:tobject);ConstBeginrow=2; Begincol =1;varexcel:olevariant;  Irow,icol:integer; Xlsfilename:string;beginif(Trim (edit1. Text) ="') Then  beginMessageBox (GetActiveWindow (), select the correct Excel path', MB_OK +mb_iconwarning);  Exit End; Xlsfilename:=Trim (edit1.  Text); TryExcel:= Createoleobject ('Excel.Application'); exceptApplication.messagebox ('Excel is not installed','Prompt Information', mb_ok+mb_iconasterisk+mb_defbutton1+mb_applmodal);  Exit; End; Excel.visible:=false;  Excel.WorkBooks.Open (Xlsfilename); TryIRow:=Beginrow; Icol:=Begincol;  whileTrim (excel.worksheets[1]. Cells[irow,icol].value) <>"'  Do begin   withADOQuery1 Do beginAppend; 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; exceptApplication.messagebox ('Error Importing Data','Prompt Information', mb_ok+mb_iconasterisk+mb_defbutton1+mb_applmodal);  Excel.quit; End; MessageBox (GetActiveWindow (),'Data import succeeded','Prompt Information', MB_OK +mb_iconwarning);End;

Delphi Excel import Generic program 2

Generic Program for Delphi Excel Import. Category: Delphi -- the- -  -: - 257People reading reviews (0) Collection Report Exceldelphiinteger database C step:1even Excel (you know its format, preferably not a field in data one by one corresponds)2read the Excel data, fill in the database I have a function here, the implementation of the Excel table data import database, before a data import to determine whether the data in the database, if so, no longer import the data (avoid duplication), you can refer to the followingprocedureTFORM_QYXXCX. Bitbtn2click (Sender:tobject);  VAR I,j:integer;  Col,row:integer;  Msexcel,wbook,wsheet:olevariant; F_temp,strtemp:string;begin  ifMain_form.lwt.Message_Confirm ('to prevent unpredictable situations (fields are too long, types are inconsistent, etc.)'+#Ten# -+'It is strongly recommended to back up the original data before importing,'+#Ten# -+'Confirm: Terminate import'+#Ten# -+'Cancel: Continue importing') Thenabort;  Cdsdjzy.open;  Cdsdjzy.first;  while  notCdsdjzy.eof DoCdsdjzy.delete;  Cdsdjzy.close;  Cdsdjzy.open; Try  beginMSExcel:= Createoleobject ('Excel.Application'); Wbook:=msexcel.application; ifOpendialog1. Execute Then   //associating to a file    begin     ifOpendialog1. Filename="'  Thenabort; Wbook.workbooks.Open (Opendialog1.    FileName); End; Wbook.visible:=true; Wsheet:=wbook.worksheets[1]; End  except  beginApplication.messagebox ('you canceled the operation or Excel is not installed!','error!', Mb_iconerror +MB_OK);  Abort; End; End; Row:=wsheet.usedrange.rows.count;//LineCol:=wsheet.usedrange.columns.count;//column  if(row=0)or(col =0) Then  beginShowMessage ('the Excel file has no data! Please confirm');  Abort End; Proform.  Show; Proform. Progressbar1.max:=Row;  withQqyb Do  beginOpen;  fori:=1  torow-1  Do    //number of rows to increase    begin   //0 Name of person 2 name of enterprise      //determine if the enterprise and personnel exist, and if present, do not import the recordstrtemp:='SELECT * from qyb where qy_name = ''+wsheet.cells[i+1,1]. Value+'"'';      Main_Form.lwt.DB_AdoQueryRun (qupdate,strtemp); ifQupdate. Recordcount<>0  Then   //exist, join a temporary table, exit this cycle      beginCdsdjzy.append; Cdsdjzy.fieldbyname ('Company Name'). Asstring: = wsheet.cells[i+1,1].        Value;        Cdsdjzy.post; Continue; End; Append; //not present, continue       forj:=0  tocol-1  Do   //column      beginProform. Progressbar1.position:=proform. progressbar1.position+1;//if Adory. FIELDS[J]. FieldkindFIELDS[J]. value:= wsheet.cells[i+1, j+1].      Value; End;    Post; End; End; Proform. Progressbar1.position:=0; Proform.  Hide; ifCdsdjzy.recordcount>1  Then  begin    ifMain_form.lwt.Message_Confirm ('data has been imported! , some records are not imported because they already exist, do you want to print an order without importing records? ') Then    beginMain_form.lwt.DB_ShowReportByDataSet (Cdsdjzy,'because there is already a message that the enterprise import failed'); Main_form.lwt.DB_Excel_Export (Cdsdjzy,'C:\hello.xls'); End; End  Else  beginMain_Form.lwt.Message_Show ('data has been imported! '); End; Wbook.workbooks.close;End;

Generic program for Delphi Excel import

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.