简体   繁体   English

从C#声明文件打开Excel表单时引发异常

[英]Exception thrown when opening a Excel spreedsheet from C# stating file is being used by another user

I am currently developing an application that requires an excel spreadsheet, location selected by the user, to be read into a DataTable and then stored in a sql server database. 我目前正在开发一个应用程序,该应用程序需要将Excel电子表格(用户选择的位置)读取到DataTable中,然后存储在sql server数据库中。

The application works perfectly in my development environment, however when it is deployed into my production environment an exception is thrown with the following message. 该应用程序可以在我的开发环境中完美运行,但是当将其部署到我的生产环境中时,会引发异常并显示以下消息。

The Microsoft Jet database engine cannot open the file '.xls'. Microsoft Jet数据库引擎无法打开文件“ .xls”。 It is already opened exclusively by another user, or you need permission to view its data. 它已经由另一个用户独占打开,或者您需要权限才能查看其数据。

My code to read the excel file is as follows: 我读取excel文件的代码如下:

OleDbConnection objConn = null;

DataSet objDataset1 = null;

string fileLocation = GetFileLocation();

string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + fileLocation
+ ";" + "Extended Properties=Excel 8.0;";

objConn = new OleDbConnection(sConnectionString);

objConn.Open(); //This is where the exception is thrown

OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1, "XLData");

DataTable dt = objDataset1.Tables["XLData"];

Note the application and excel file are on different servers in the same domain. 请注意,应用程序和excel文件位于同一域中的不同服务器上。

Digging around various forumns and knowledge bases it would appear that the exception is thrown when the "user" does not have permission to use the file. 挖掘各种论坛和知识库,当“用户”无权使用该文件时,似乎会引发异常。 Although not recommended permissions on the file have been set to Full Access for all users. 尽管不建议将所有用户对该文件的权限设置为“完全访问”。

Apart from file permissions what else could cause this exception to be thrown? 除了文件许可权之外,还有什么可能导致引发此异常?

Not sure if this is helpful but... 不确定这是否有帮助,但是...

The error: 错误:

The Microsoft Jet database engine cannot open the file '.xls'. Microsoft Jet数据库引擎无法打开文件“ .xls”。

seems to point to a file named .xls , as opposed to something named, say myspreadsheet.xls or prodfile.xls . 似乎指向一个名为.xls的文件,而不是一个名为myspreadsheet.xlsprodfile.xls

It may be a long shot, but can you do some debugging to make sure that the file Jet is trying to open actually exists? 这可能会花费很长时间,但是您可以进行一些调试以确保Jet试图打开的文件确实存在吗? It may be that the filename is not being constructed properly, for some reason. 出于某种原因,可能是文件名构造不正确。

Here's a few answers to the same root problem: 以下是针对相同根本问题的一些答案:

Read from Excel using OleDb in a Windows Service? 在Windows服务中使用OleDb从Excel中读取?

kill the previous opened excel process before starting the new process for reading the excel.it will solve your problem. 在开始阅读excel的新过程之前,请杀死先前打开的excel过程。它将解决您的问题。

i wrote one function you can call it before opening the xls. 我写了一个函数,可以在打开xls之前调用它。

private void kill_excel_process()
    {      
        foreach (Process clsProcess in Process.GetProcesses())
        {
            if (clsProcess.ProcessName.Equals("EXCEL"))
            {
                clsProcess.Kill();
                break;
            }
        }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM