通过OLEDB连接到Excel时文件权限错误

时间:2011-06-23 16:47:23

标签: c# excel oledbconnection

以下代码在我第一次访问文件以进行行计数时正常工作。但是,当我尝试打开同一个文件来读取数据时,我会在ExcelConnection.Open()上抛出一个错误,指出我没有访问该文件的权限或者它已被使用。有没有想过为什么文件在第一次连接后没有被释放?

public static DataSet GetExcelWorkSheet(string pathName, string fileName)
        {
            string fileExtention = System.IO.Path.GetExtension(fileName).ToLower();

            OleDbConnection ExcelConnection = new OleDbConnection(fileExtention == ".xls" ?
                @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + @"\" + fileName + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1;ReadOnly=true;\"" :
                @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + @"\" + fileName + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1;ReadOnly=true;\"");
            OleDbCommand ExcelCommand = new OleDbCommand();
            ExcelCommand.Connection = ExcelConnection;
            OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
            ExcelConnection.Open();

            // DataTable ExcelSheets = ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            DataTable ExcelSheets = ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);

            string CheckSheetName = ExcelSheets.Rows[0]["TABLE_NAME"].ToString();
            string SpreadSheetName;
            int useMe = -1;

            if (CheckSheetName.ToUpper() != "SHEET1$")                 // Ok, they have renamed things
            {
                if (CheckSheetName.Substring(0, 5).ToUpper() == "SHEET")
                // if it does START with sheet then look for anything that
                // DOES NOT start with sheet
                {
                    for (int x = 0; x < ExcelSheets.Rows.Count; x++)
                    {
                        if (ExcelSheets.Rows[x]["TABLE_NAME"].ToString().Substring(0, 5).ToUpper() != "SHEET")   // If is does not equal sheet then use it
                        {
                            useMe = x;
                        }
                    }
                }
            }

            SpreadSheetName = string.Format("[{0}]", useMe == -1 ? CheckSheetName : ExcelSheets.Rows[useMe]["TABLE_NAME"].ToString());

            try
            {
                DataSet ExcelDataSet = new DataSet();
                ExcelCommand.CommandText = @"SELECT * FROM " + SpreadSheetName;
                ExcelAdapter.Fill(ExcelDataSet);
                return ExcelDataSet;
            }
            catch (Exception)
            {
                ExcelConnection.Close();
                return new DataSet();
            }
            finally
            {
                // Clean up.
                if (ExcelConnection != null)
                {
                    ExcelConnection.Close();
                    ExcelConnection.Dispose();
                }
                if (ExcelSheets != null)
                {
                    ExcelSheets.Dispose();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您必须在读取文件或完成odne操作后关闭连接.... Excel Connection.Close();