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

查看:136
本文介绍了在Windows服务中使用OleDb从Excel读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我知道这是一件糟糕的事情。这是我们客户唯一的选择。

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

问题:

我们需要从Excel文件每x个时间。数据不断变化通过第三方Excel插件。应用程序的环境是Windows XP,SP1和.Net 2.0。不将操作系统升级到SP2 / 3或升级.Net Framework。再次,这是每个客户的参数。

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

从控制台或Windows窗体运行时,此代码可以工作,但是当我们从Windows服务运行它时,它无法访问Excel文件。 XP中的限制是否与Vista中的桌面应用程序无法进行交互?这可以从服务中完成吗?从服务器运行此服务的可靠性非常需要客户端。

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.

编辑:

服务是作为LocalSystem运行。

The service is running as LocalSystem.

编辑#2:

从服务运行时得到的错误是: Microsoft Jet数据库引擎无法打开文件'。它已经由另一个用户专门开放,或者您需要查看其数据的权限。

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.

Excel文件在桌面上打开,但必须打开从第三方应用程序获取更新。

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

推荐答案

除了@ sontek的答案。

In addition to @sontek's answer.

[仅适用如果您是有问题的电子表格的所有者/开发人员]]

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

您可能会发现使用数据推送模式而不是数据拉模式更容易。根据插件的一些事件,通常将一个简单的编程模型通过excel宏直接推送到数据存储中。这通常比定时器上的服务轮询更好。

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.

这篇关于在Windows服务中使用OleDb从Excel读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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