C# 4.0 用c.net从Excel导入Access数据库

C# 4.0 用c.net从Excel导入Access数据库,c#-4.0,C# 4.0,我正在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=Mic

我正在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\u excel”。它已被其他用户以独占方式打开,或者您需要查看其数据的权限

此外,我不知道扩展属性的含义。我正在使用Microsoft Office 2007软件包。如果我将扩展属性设置为7.0,则会出现以下错误:

找不到可安装的ISAM

请提供一些代码示例来帮助我们


提前感谢。

检查连接字符串是否适合您的Access数据库版本

详情请浏览


Mohamed Inshafudeen J.

错误:找不到可安装的ISAM

1:将excel文件另存为excel 97-2003工作簿 2:并包括参考Microsoft.Office.Interop.Excel


祝你好运

运行代码时,文件是否在excel中打开?