简体   繁体   中英

MS Access/VB.Net - how can I insert rows from an Excel sheet data into MS Access table?

My program is written in VB.NET and I have an excel sheet with only one column and type of column is Text. Now I want to add all the rows of excel sheet into MS Access table.

My Access table has 5 columns, and I have to provide some static data for rest of fields. I can't let them empty as my table structure won't allow me to let them empty. they are mandatory.

My algorithm works this way

  • Read excel sheet data into a DataTable
  • Loop to the end of all rows of DataTable
  • Append text of each cell and some of my static information to make a query string

     INSERT into TableName(c1,c2,c3) values (v1,v2,v3), (v1,v2,v3), (v1,v2,v3), (v1,v2,v3) ... ... (n1,n2,n3); 
  • Execute this query

But I got error in this query, Any suggestions?

You can run SQL against a connection to interact directly with Access and Excel. For example, working with the connection:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\Test.xlsm;
Extended Properties="Excel 12.0 xml;HDR=Yes;";

Or Jet

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Z:\Test.xls;
Extended Properties="Excel 8.0;HDR=Yes;";

You can run the following SQL:

INSERT INTO [;DATABASE=Z:\Docs\Test.accdb].Table1 (ID,Atext) 
SELECT ID, AText FROM [Sheet7$] 

Or Jet

INSERT INTO [;DATABASE=Z:\Docs\Test.mdb].Table1 (ID,Atext) 
SELECT ID, AText FROM [Sheet7$] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM