【问题标题】:importing of data from Excel To Access database by c#.net通过 c#.net 从 Excel 导入数据到 Access 数据库
【发布时间】:2011-04-09 19:43:42
【问题描述】:

我正在 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 软件包。如果我设置 Extended Properties = 7.0,则会出现以下错误:

找不到可安装的 ISAM。

请提供一些代码示例帮助。

提前谢谢..

【问题讨论】:

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

标签: c#-4.0


【解决方案1】:

检查连接字符串对于您的 Access 数据库版本是否正确。

详情请访问ConnectionString.com

Mohamed Inshafudeen J.

【讨论】:

    【解决方案2】:

    对于错误:找不到可安装的 ISAM。

    1:将您的excel文件保存为“Excel 97-2003 wokbook” 2:还包括参考“Microsoft.Office.Interop.Excel”

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-31
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多