日期:2014-05-17  浏览次数:20566 次

导入Excel的问题,大家来看看啊。。
今天在导入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的代码
C# code

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();
        }




------解决方案--------------------
会不会是文件格式不对引起的 提供一个方法给你参考下
C# code

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)

}

------解决方案--------------------
注意access权限设置
------解决方案--------------------
可能是Excel连接字符串写错了