The following code is from http://blog.csdn.net/hellogv/. for reference, see the source!
First, this article is intended for friends with database development experience. If you think that this article is not helpful to you, do not laugh. The following code is written by the younger brother.
Before importing Excel data to the database, perform some simple preparation:
1. Save the Excel table in DBF 4 format.
2. Open BCB and add the adotable (renamed dbftable) and datasource controls.
OK. The preparation is over. The rest is code.
1. In the form_load () event, add the following code:
Ansistring filepath = extractfilepath (filename); // filename is the DBF File Name ansistring tablename = extractfilename (filename ). substring (0, extractfilename (filename ). length ()-4); // remove the file extension
// -------------------- Connect to the DBF File ---------------------------------- dbftable-> connectionstring = "provider = Microsoft. jet. oledb.4.0; Password =/"/"; Data Source =/"" + filepath + "/"; extended properties = DBASE 5.0; persist Security info = true "; dbftable-> tablename = tablename; dbftable-> open ();
2. Create a class member function void _ fastcall exceltodb (tadoquery * adoquery );
The specific implementation of the function is as follows:
Void _ fastcall tdbftodbform: exceltodb (tadoquery * adoquery) {// The implementation is very simple, just add dbftable data to the local database dbftable-> first () one by one (); for (Int J = 0; j <dbftable-> recordcount; j ++) {adoquery-> append (); For (INT I = 0; I <dbftable-> fieldcount; I ++) adoquery-> fieldbyname (dbftable-> fieldlist-> strings [I])-> asstring = dbftable-> fieldbyname (dbftable-> fieldlist-> strings [I]) -> asstring; adoquery-> post (); dbftable-> next ();} showmessage ("Export Inbound successful! ");}
Note: dbftable is used to load the data stored in another Excel file, while adoquery is used to load the data in the program itself (of course, you can also change it ),