Excel文件-另一个用户已经专门打开了该文件, [英] Excel file - It is already opened exclusively by another user,

查看:369
本文介绍了Excel文件-另一个用户已经专门打开了该文件,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#阅读excel文件,并且下面的代码按预期工作,除了每次我运行该应用程序外,我都必须关闭excel文件,否则会收到以下错误消息:

I am reading the excel file using C# and below is the code which is working as expected EXCEPT that every time i run the app, I have to close the excel file otherwise I get the below error message:

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..

我的问题是:完成阅读后,是否可以关闭excel文件?

my question is: is there a way i close the excel file once i am done reading?

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

推荐答案

使用工作表名称代替工作簿名称

use sheet1 name instead of workbook name

这篇关于Excel文件-另一个用户已经专门打开了该文件,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆