简体   繁体   中英

Read from Excel using OleDb in a Windows Service?

Disclaimer: I know this is a bad way to do things. It is the only option we have with our client.

Problem:

We need to read data from an Excel file every x amount of time. The data is constantly changing via a 3rd party Excel plug in. The environment for the application is Windows XP, SP1 and .Net 2.0. No upgrading the OS to SP2/3 or upgrading the .Net Framework. Again, this is all per customer parameters.

Here is current code we have:

String sConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Settings.ReutersFilePath + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";
using (OleDbConnection objConn = new OleDbConnection(sConnectionString))
{
    try
    {
        objConn.Open();
        _dataSet = new DataSet();
        OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [" + ConfigurationManager.AppSettings["Excel.WorksheetName"] + "$]", objConn);
        OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
        objAdapter1.SelectCommand = objCmdSelect;
        objAdapter1.Fill(_dataSet);
        _dataTable = _dataSet.Tables[0];
        objConn.Close();

        // Persists new data to a database.
        UpdateSQL();
    }
    catch (Exception ex) { }
    finally
    {
        _isCurrentlyProcessing = false;
        if (objConn != null && (objConn.State != ConnectionState.Broken || objConn.State != ConnectionState.Closed))
        {
            objConn.Close();
        }
    }
}

This code works when running from console or a Windows form, but when we run it from a Windows service, it cannot access the Excel file. Is there a limitation in XP w/SP1 that doesn't allow interaction with desktop applications like there is in Vista? Can this be done from a service? The reliability of running this from a service is much needed from the client.

Edit:

The service is running as LocalSystem.

Edit #2:

The error I get when running from a service is: The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.

The Excel file IS open on the desktop, but it has to be open for it to get updates from the 3rd party application.

In addition to @sontek's answer.

[only applicable If you are the owners/developers of the Spreadsheet in question...]

You may find it easier to work with a "data push" model rather than a "data pull" model. It's usually an easier programming model to push the data via an excel macro directly into your data store, based upon some event from your plug-in. this is often a much better scenario than a service polling on a timer.

Excel doesn't allow you to open a file simultaneously. You need to either create it as a shared workbook or make a copy of the file and work off of the copy.

There is also full excel API (Microsoft.Office.Interop.Excel) that you could try using, which would allow you to hook into the currently opened excel/workbook.

What user is the service running as? Make sure that user has access to the specific file.

It seems most likely that it is a permissions issue. Windows services run under a different user account than desktop applications.

Also, you mention that you are executing this method every x amount of time. Make sure you are using the right timers. http://www.aspfree.com/c/a/C-Sharp/Timer-Objects-in-Windows-Services-with-C-sharp-dot-NET/

And you should be able to attach a debugger to the service by using "Attach to Process" and selecting your host process. This might give you more info on your problem. http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx

SpreadsheetGear for .NET can open a file even if Excel currently has the file open and it is well suited to using from a Windows Service.

However, the file on disk will not reflect changes which have not been saved by Excel, and SpreadsheetGear would not be able to open a workbook while Excel is in the middle of saving, so the service would have to take that into account.

Disclaimer: I own SpreadsheetGear LLC

You could try making it a shared workbook (Tools > Share Workbook). This lets multiple users edit the same workbook concurrently. I'm not sure if that would work for you as some workbook features get locked down when it is shared

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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