在C#中以编程方式将数据从Excel文件导入到Access

时间:2013-02-08 14:46:00

标签: database excel ms-access

鉴于Excel和Access在某种程度上是兼容的,有没有办法以编程方式将数据从excel文件导入Access数据库,而无需读取文件和插入数据,按记录进行记录?

1 个答案:

答案 0 :(得分:4)

非常粗略:

string ConnString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\Docs\Test.accdb";
OleDbConnection conn = new OleDbConnection(ConnString);
conn.Open();
string sql = @"select * into newxl from [Excel 8.0;HDR=YES;DATABASE=Z:\Docs\Test.xlsx].[Sheet1$] s;";

OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();