我正在使用c#.net中的Windows应用程序,我需要通过使用c#中的代码将Excel工作表导入Access数据库。我在网上找到了以下代码并尝试使用:
string path = @"D:\project_excel";
OleDbConnection con;
System.Data.DataTable dt = null;
//Connection string for oledb
string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path + "; Extended Properties=Excel 8.0;";
con = new OleDbConnection(conn);
try
{
con.Open();
dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
String[] excelsheets = new String[dt.Rows.Count];
int i = 0;
foreach (DataRow dr in dt.Rows)
{
excelsheets[i] = dr["TABLE_NAME"].ToString();
i++;
}
// here i manaually give the sheet number in the string array
DataSet ds = new DataSet();
foreach (string temp in excelsheets)
{
// Query to get the data for the excel sheet
//temp is the sheet name
string query = "select * from [" + temp + "]";
OleDbDataAdapter adp = new OleDbDataAdapter(query, con);
adp.Fill(ds, temp);//fill the excel sheet data into a dataset ds
}
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
但是它提供了一个例外情况,如下所述:
Microsoft Jet数据库引擎无法打开文件“D:\ project_excel”。它已由其他用户专门打开,或者您需要获得查看其数据的权限。
此外,我不知道扩展属性的含义。我正在使用Microsoft Office 2007包。如果我设置扩展属性= 7.0,则会出现以下错误:
无法找到可安装的ISAM。
请提供一些代码示例。
提前致谢..
答案 0 :(得分:0)
答案 1 :(得分:0)
对于错误:找不到可安装的ISAM。
1:将excel文件另存为“Excel 97-2003 wrokbook” 2:还包括引用“Microsoft.Office.Interop.Excel”
祝你好运!