【发布时间】: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