Required Components:
Microsoft ADO Ext. 2.8 for DDL and security or updated components.
Add to:
Using ADOX;
Using System.Runtime.InteropServices;
Using System.IO;
Then take advantage of the OleDbCommand component, set its 2 links, a link to find and open the Excel data source, and another link to insert the data source into the MDB file.
Operation Page:
Background Source:
Private voidButton1_Click (Objectsender, EventArgs e) { varMdbpath = This. Txtmdbpath.text; if(File.exists (mdbpath) = =false) {ADOX. Catalog Catalog=NewADOX. Catalog (); Dynamic CN =NULL; Try{cn= Catalog. Create (string. Format ("Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};", Mdbpath)); } finally { if(CN! =NULL) {Marshal.finalreleasecomobject (CN); } marshal.finalreleasecomobject (catalog); } stringExcelpath = This. Txtexcelpath.text; stringExcelconnection =string. Format ("Provider=microsoft.ace.oledb.12.0;data source={0}; Extended properties=\ "Excel 8.0; Hdr=yes\ "", Excelpath); using(System.Data.OleDb.OleDbConnection Accessconn =NewSystem.Data.OleDb.OleDbConnection (excelconnection)) {Accessconn.open (); stringInsertcommandtext =string. Format ("SELECT * into [MS access;database={0}]. [Sheet1] from [sheet1$];", Mdbpath); using(System.Data.OleDb.OleDbCommand Accesscommand =NewSystem.Data.OleDb.OleDbCommand (Insertcommandtext, Accessconn)) {accesscommand.executenonquery (); } accessconn.close (); }} MessageBox.Show ("The data was written successfully. "); }
Note: The Data Source link section in Insertcommandtext must be written: database= ..., cannot write data source= ..., otherwise a "table already exists" exception appears.
Convert Excel data to MDB file