导入Excel的问题,大家来看看啊。。

yinsuxia 2012-08-22 02:51:11
今天在导入Excel的时候,报错:The Microsoft Access database engine cannot open or write to the file 'D:\Daniel\StarBucksFulfillments\WebSite\UpLoad\GoldCardReplacement\'. It is already opened exclusively by another user, or you need permission to view and write its data.我这个Excel文档没有打开啊,请问这是什么问题呢??附上导入Excel的代码

public DataSet Execl()
{
DataTable dt=GetSchemaTableName();
ArrayList list = new ArrayList();
foreach (DataRow item in dt.Rows)
{
list.Add(item["TABLE_NAME"].ToString());
}
DataSet Myds = new DataSet();
OleCon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
OleCon.Open();

OleCmd.CommandText = "select * from [" + list[0] + "]";
OleCmd.Connection = OleCon;
OleDa.SelectCommand = OleCmd;

//DataTable dt = new DataTable();




try
{
//OleCon.ConnectionString = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + FileName + ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'";
OleDa.Fill(Myds, "Execl");
return Myds;
//OleCon.Open();
//dt = OleCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
//return dt;
}
catch(Exception e)
{
return Myds;
//return dt;
}
finally
{

OleCon.Close();
OleCmd.Dispose();
OleDa.Dispose();
OleCon.Dispose();
}

...全文
91 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
清茶&浊酒 2012-08-22
  • 打赏
  • 举报
回复
可能是Excel连接字符串写错了

//2007或者2010版本的Office  
//string conn_str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + "; Extended Properties=Excel 12.0;";
//2003的Office
string conn_str = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";




我的博客里有个写好的Excel 导入数据库的类你可以看下~~~~
EnForGrass 2012-08-22
  • 打赏
  • 举报
回复
注意access权限设置
风2013 2012-08-22
  • 打赏
  • 举报
回复
会不会是文件格式不对引起的 提供一个方法给你参考下

Public DataSet ds(){

string type2 = fu_fileuplaod.FileName;
var type3 = type2.Substring(type2.LastIndexOf('.'));

if (!type3.Equals(".xls") && !type3.Equals(".xlsx"))
{
RegisterScriptAlert("上传excel文档的格式与所要求的格式不一致");
return;
}

string newName = Server.MapPath("..//App_Data//Uploads//") + DateTime.Now.ToString("hhmmss") + ".xls";

fu_fileuplaod.SaveAs(newName);
string connStr = string.Empty;

if (type3.Equals(".xlsx"))
{
connStr = "Provider=Microsoft.Ace.OleDb.12.0;Data Source=" + newName +
";Extended Properties='Excel 12.0 Xml; HDR=YES; IMEX=1'";
}

else if (type3.Equals(".xls"))
{
connStr = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + newName +
";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
}
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State.ToString() == "Closed")
{
conn.Open();
}

DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

ArrayList list = new ArrayList();
foreach (DataRow item in dt.Rows)
{
list.Add(item["TABLE_NAME"].ToString());
}
var str11 = "select * from [" + list[0] + "]";
OleDbDataAdapter oda = new OleDbDataAdapter(str11, conn);
DataSet ds = new DataSet();
oda.Fill(ds);
conn.Close();
File.Delete(newName)

}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧