使用 .NET 与 MS Excel 进行 COM 互操作的(免费)替代方案是什么(如果有的话)?

发布于 2025-01-06 22:27:37 字数 950 浏览 0 评论 0原文

背景:我正在尝试决定是否走上通过 Interop 使用 C# 自动化 Excel 的道路。我当然不是 VBA 人员,并且出于多种原因,我不想走这条路。我对其他选择不太了解,而且我真的不想买任何东西。

性能非常重要。

我试图找到有关使用 Interop 自动化 Excel 的参考资料,但似乎相当稀疏​​。一些更好的东西已经已过时,而其他参考文献只是不涵盖很多材料。许多人指出,许多现有材料都花时间关注 MS Office 版本的差异。我还想知道迁移到 .NET 4 所带来的改进是否具有实质性。

编辑:我想要完成的关键是相当复杂的格式化以及一些相当繁重的分析(线性回归)的完全自动化。我需要在分析/计算后对数据进行图表化,图表格式非常重要,因为与我一起工作的 MBA 都这么说。我想所有这一切都可以通过 VBA 实现,但我不确定,因为我不是 VBA 人员。考虑隐藏应用程序的宏类型内容,以便它作为后台进程运行。性能至关重要(这是我质疑 COM 是否合适的原因之一)。由于性能方面的考虑,VBA 甚至可能被排除在外。

现在,回答这个问题。

我可以简洁地提出这个问题的最佳方式是:是否有一份清单列出了您无法使用 Excel 中的 Interop 执行但您希望能够执行的操作?在您的开发经验中,您是如何解决这个问题的?

如果确实没有什么是 Interop 做不到的(请参阅下面的评论),并且它最适合我想要做的事情,学习使用它的最佳方法是什么

如果我有更好的方法来解决上述问题,您能告诉我吗?有人提到了 Excel DNA,但我不认为它比允许创建可以访问后端服务器应用程序或数据库的用户定义的 C# 函数更有帮助。

Background: I'm trying to make a decision on whether to go down the road of automating Excel using C# via Interop. I'm certainly not a VBA guy and for numerous reasons, I'd prefer not to go down that road. I don't know much about other options, and I'm really not looking to buy anything.

Performance is massively important.

I've tried to find references on using Interop to automate Excel and it seems rather sparse. Some of the better stuff is dated and other references just don't cover a lot of the material. Many folks cite the fact that a lot of the existing material spends time focusing on differences in version of MS Office. I'm also wondering whether the improvements that have been claimed with moving to .NET 4 are material.

Edit: The crux of what I'm trying to accomplish is complete automation of fairly involved and intricate formatting as well as some pretty heavy analysis (linear regression). I need to chart data once analyzed/calculated and the chart formatting is really important because the MBA's I work with say it is. I guess all of this is possible via VBA but I don't know for sure because I'm not a VBA guy. Think macro type stuff with the app hidden so it's running as a background process. Performance is critical (one of the reasons I question COM's appropriateness). VBA may even be off the table due to its performance considerations.

Now, for the question.

The best way I can ask this succinctly is: is there a laundry list of stuff you can't do with Interop in Excel that you'd like to be able to do? How have you gotten around it in your development experiences?

If there really isn't anything you can't do with Interop (see comments below) and it's the best fit for what I'm intending to do, what is the best way to go about learning to use it given that COM and the Excel object model can be a fairly intimidating undertaking. Are there really good references out there?

If there's a better way for me to go about the above, can you tell me about it? Excel DNA was mentioned but I can't see it being much more helpful than allowing the creation of user-defined C# functions that can get to a backend server app or database somewhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2025-01-13 22:27:38

您的问题没有正确答案:这完全取决于细节和背景。

由于 COM 之上有额外的层,与 Excel 对象模型(获取/放置数据格式化图表等)交互时的互操作性能始终相对较慢。
.Net 计算速度可以比 VBA 计算速度更快,尤其是在 VBA 代码写得不好的情况下。
.NET 框架比 VBA 框架更强大、更复杂。
Excel 对象模型庞大且复杂。 VBA 宏记录器对此很有帮助,但会产生令人震惊的代码。
XLL 接口比 COM 更快,但暴露的 Excel 对象模型更少。

根据您的描述,我的猜测是,使用 VBA 而不是 .NET,您会获得更好的性能、更易于学习和使用,因为它似乎可能大部分时间都花在与对象模型交互上,而不是进行线性回归计算(恕我直言,这不是特别 CPU 密集型的)。
但是当然,其他考虑因素可能会让您更喜欢 .NET

There is no correct answer to your questions: it all depends on the details and the background.

Interop performance when interacting with the Excel object model (get/put data formatting charts etc) will always be relatively slow because of the additional layers on top of COM.
.Net calculation speed can be faster than VBA calculation speed particularly with poorly written VBA code.
The .NET framework is much more powerful and complex than the VBA framework.
The Excel object model is large and complex. The VBA macro recorder can be helpful with this but produces apalling code.
The XLL interface is faster than COM but exposes less of the Excel object model.

Given your description my guess is that you would get better performance, ease of learning and use with VBA rather than than .NET because it seems likely that the majority of time will be spent interacting with the object model rather than doing linear regression calculations (which are not IMHO particularly CPU intensive).
But of course other considerations might make you prefer .NET

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文