Excel文件 – 它已经由另一个用户专门打开,

我正在阅读使用C#的Excel文件,下面是正在按预期工作的代码除了每次运行应用程序,我必须closuresExcel文件,否则我会得到下面的错误信息:

The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data..

我的问题是:有没有办法我closuresExcel文件,一旦我读完了?

 public static DataTable LoadExcelWorkbook(string workbookName) { OleDbConnection connection; string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", EXCELFILENAME); string query = String.Format("select * from [{0}$]", workbookName); using(OleDbConnection conn = new OleDbConnection(connectionString)) { connection = new OleDbConnection(connectionString); connection.Open(); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString); DataSet dataSet = new DataSet(); dataAdapter.Fill(dataSet); DataTable myTable = dataSet.Tables[0]; dataAdapter.Dispose(); connection.Close(); dataSet.Dispose(); //CLOSE THE EXCEL FILE????????? if (myTable != null) return myTable; return null; } } 

使用sheet1名称而不是工作簿名称