如果读取excel文件时出错,之后再运行程序就提示文件访问被拒绝,有其他用户在使用。
如何解决呢?

解决方案 »

  1.   

    OleDbConnection myConn = new OleDbConnection(strCon);//创建一个OLEDBC的链接
    myConn.Open();//打开数据库的连接,得到一个数据集
    DataSet myDataSet = new DataSet();
    string StrSql = "Select * From [" + StyleSheet + "$]";
    OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
    myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
    myCommand.Dispose();
    DataTable dt = myDataSet.Tables["[" + StyleSheet + "$]"];
    myConn.Close();//关闭数据库连接
    myCommand.Dispose();
      

  2.   

    把myConn.Close();放到finally里面试试
      

  3.   

    FileStream操作excel文件.再释放资源
    或多线程操作
      

  4.   

    OleDbConnection myConn = new OleDbConnection(strCon);//创建一个OLEDBC的链接
                myConn.Open();//打开数据库的连接,得到一个数据集
                DataSet myDataSet = new DataSet();
                string StrSql = "Select * From [" + StyleSheet + "$]";
                OleDbDataAdapter myCommand = new OleDbDataAdapter(StrSql, myConn);
                myCommand.Fill(myDataSet, "[" + StyleSheet + "$]");
                myCommand.Dispose();
                DataTable dt = myDataSet.Tables["[" + StyleSheet + "$]"];
                myConn.Close();//关闭数据库连接
                myCommand.Dispose();
      

  5.   

    如果读取excel文件时出错,之后再运行程序就提示文件访问被拒绝,有其他用户在使用。
    如何解决呢?看你是怎么读取的!如果使用类似这样的方式:
    public DataSet ExcelToDS(string Path)
    {
     string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";
     OleDbConnection conn = new OleDbConnection(strConn);
     conn.Open();  
     string strExcel = "";   
     OleDbDataAdapter myCommand = null;
     DataSet ds = null;
     strExcel="select * from [sheet1$]";
     myCommand = new OleDbDataAdapter(strExcel, strConn);
     ds = new DataSet();
     myCommand.Fill(ds,"table1");   
     return ds;
    }
    那么你应该确保释放conn!如果你是使用vsto,则需要调用Excel.Application.Exit();