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

查看:156
本文介绍了在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框架。再次,这都是每个客户的参数

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的限制瓦特/ SP1,不允许与桌面应用程序的交互就像没有在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.

推荐答案

在除了@硕腾的回答。

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天全站免登陆