如何创建读取自定义数据的 Excel 加载项?
我的任务是为 Excel 2007 创建一个插件,用于从自定义数据源读取数据。我已经有了读取数据的代码。它用 C# 编写并返回对象。这是针对银行系统的,我无法直接访问数据。必须向 C# DLL 提供用户 ID、密码和硬件生成的密钥才能获取最新数据。
我的老板希望数据每分钟自动更新一次,或者使用 Excel 提供的刷新功能手动更新。因此,我需要使数据显示在 Excel 中,就像来自标准数据库连接一样。
我已经四处寻找一个起点,但我正在努力寻找一些有助于满足这一非常具体的要求的信息。有人可以帮忙吗?
I have been tasked with creating an addin for Excel 2007 that reads data from a custom data source. I already have the code that reads the data. It's written in C# and returns objects. This is for a banking system and I cannot give direct access to the data. A userID, password and hardware generated key must be provided to the C# DLL to get up to date data.
My boss wants the data to be updated automatically once per minute or manually using the Refresh function provided by Excel. So I need to make the data appear in Excel as if it were from a standard database connection.
I've looked around for a starting point but I am struggling to find some information that will help with this very specific requirement. Can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先下载 VSTO (Visual Studio Tools for Office) 这将允许您创建 C# Excel 加载项。
在 Visual Studio 中,当您创建新项目时,您将看到 Office,并且可以从中选择 Excel。
从那里开始,一旦你这样做了,你就可以回来提出更具体的问题。
使用 Excel 的一些有用技巧。
选择活动工作表:
选择特定范围(本例中为 A1 - B5):
将值设置为整个范围:
您可以从二维数组中的范围获取值,例如:
然后您可以更改整个数组中的值并应用回范围:
您可以使用此通过首先准备一个数组然后将其设置为范围值来一次更新整个范围的技术。
要从范围内的特定单元格获取值(设置值的方式相同,但相反)。
这应该允许您在 Excel 中执行基本任务,您可以设置值、获取值以及选择范围。一旦您有更具体的问题,请回来。
不要忘记,来自 Excel 的数组是从 1 开始的,而不是从 0 开始的!!!
Begin with downloading VSTO (Visual Studio Tools for Office) this will allow you to created a C# Excel Add-In.
In Visual Studio when you create a new project you will see Office and you will be able to select Excel from it.
Start from there, once you do that you can come back and ask more specific questions.
Some useful tips with working with Excel.
To Select an Active Sheet:
To Select a specific range (A1 - B5 in this case):
To Set a value to the whole range:
You can get values from range in a 2 dimensional array, for instance:
You can then change the values in the whole array and apply back to the range:
You can use this technique to update the whole range at once, by preparing an array first and then setting it to the range values.
To get a value from a specific cell in your range (To set a value is the same way but reverse).
This should allow you to do basic tasks in Excel, you can set values and get values and select ranges. Once you have more specific question, please come back.
Don't forget, arrays coming from Excel are 1-based not zero-based !!!
您可以尝试使用XLLoop。这允许您在服务器上以各种语言创建 Excel 函数 (UDF)。因此,您可以编写返回数据的函数 - 用户将使用 Shift-F9 来刷新。
这里有一个基本的 C# 服务器实现:
http://winrun4j.cvs.sourceforge.net /viewvc/winrun4j/org.boris.xlloop/servers/csharp/
顺便说一句,我在该项目上工作,所以如果您有任何问题,请告诉我。
You could try using XLLoop. This allows you to create excel functions (UDFs) in various languages on a server. So you could write function that returns the data - the user would use Shift-F9 to refresh.
There is a basic C# server implementation here:
http://winrun4j.cvs.sourceforge.net/viewvc/winrun4j/org.boris.xlloop/servers/csharp/
BTW, I work on the project so let me know if you have any questions.
要自动更新,您需要创建一个实现 Excel RealTimeData (RTD) COM API 的 DLL。请参阅https://support.microsoft.com/en-us/kb/285339。
由于您想在 C# 中执行此操作,因此 Excel DNA (https://exceldna.codeplex.com/) 是最简单的起点,将为您完成大部分工作。
To auto-update, you will want to create a DLL that implements the Excel RealTimeData (RTD) COM API. See https://support.microsoft.com/en-us/kb/285339.
Since you want to do it in C#, Excel DNA (https://exceldna.codeplex.com/) is the easiest place to start and will do most of the work for you.