使用 F# 自动化 Excel 2010
我一直在寻找常见问题解答来告诉我如何打开 Excel 工作簿/工作表以及如何在完成后保存文件。
我注意到,在大多数常见问题解答和我在 F# 上购买的所有书籍中,一本都展示了如何创建新的工作簿/工作表,但从未展示如何打开或保存它。
作为 F# 的新手,如果有人能给我一个答案或者一些指示,我将非常感激。
更新
至于为什么是F#而不是C#或VB?
我很高兴地说,尽管我是一个新手(Forth、VBA 和 Excel 2003、2007 和 2010 以及 Visual Basic 除外),但我可以在 VB、VBA 和 Visual Basic 中完成此操作。 C#,自从我因医学原因退休后,不幸的是我手头有很多时间,我喜欢不断地给自己设定挑战,以保持我的小灰细胞活跃,并成为尝试新语言的傻瓜......好吧!
F# 现在是 Visual Studio 2010 的一个组成部分,所以我想 - 为什么不呢。考虑一下这一点 - 如果我们不愿意使用或至少尝试一种新语言 - 我总是想知道我是否可能更喜欢它而不是 VBA、VB、C# ..... 如果你从另一个角度来看它视图,如果没有人会使用它 - 为什么首先要创建它?我想你可以说,如果穴居人没有尝试通过将两根棍子摩擦在一起生火——我们现在会在哪里,火柴会被发明吗?
虽然完整的答案很好,但我更喜欢一些提示,以继续我的挑战。
最后但并非最不重要的一点是——感谢您抽出宝贵的时间来回复!
I have been searching for a FAQ to tell me how to open a Excel Workbook/Worksheet and also how to Save the File once I have finished.
I notice that in most FAQ and all the books I have purchased on F# one is show how to create a new Workbook/Worksheet but is never shown how to either open or Save it.
Being a newbie to F# I would very much appreciate it if anyone could kindly provide me with either an answer or perhaps a few pointers?
Update
As for why F# and not C# or VB?
I am pleased to say that inspite of being a newbie (with the exception of Forth, VBA & Excel 2003, 2007 & 2010 and Visual Basic) I can do this in both VB, VBA & C# and since I've been retired on medical grounds, with plenty of time unfortunately on my hands, I like to continually set myself challenges to keep my little grey cells active and being a sucker for trying new languages....well!
F# is now an intergral part of Visual Studio 2010 so I thought - why not. Consider this - if we are not willing to use or at least try a new languages - I would always be wonder if I might have prefer it to VBA, VB, C# ..... and if you look at it from another point of view, if no one is going to use it - why create it in the first place? I suppose you can say if cave men hadn't experimented and made fire by rubbing two sticks together - where would we be now and would matches have been invented?
Although an complete answer would be good, I prefer a few pointers, to keep my challenge going.
And lastly but not least - thank you for taking the trouble to respond!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为它们是 Office 的特定 F# 库,因此您只需使用与 VB.NET/C# 中使用的完全相同的 .NET 库。 F# 是一种 .NET 语言,因此可以在 C# 中完成的任何操作都可以在 F# 中完成(但您可能已经知道了:))。 API 调用将完全相同,只是它们将使用 F# 语法而不是 VB/C# 语法完成。例如,看起来像这样的内容
将在 F# 中表示为
现在,您很快就会意识到 API 的设计并不是为了从函数式语言中轻松使用。这是可以完成的,但这个任务尤其适合 C#/VB.NET。
如果你真的想享受F#,我建议你在它真正发挥优势的地方使用。我个人的经验是,当涉及大量数学时,函数式语言非常棒。如果您想在应用程序中轻松引入并行性,这也是很棒的(因为 F# 代码通常没有副作用)。因此,任何需要对大量数据进行数据处理的事情都是完美的。但对于主要包含对外部库的一堆 API 调用的任务,F# 有点乏味。您可以说 F# 有点像图形卡编程语言,而 C# 是通用 CPU 编程语言。很多东西在 C# 上运行得更好,但在 F# 上运行得更好的东西在 F# 上运行得确实更好。
但如果您确实想走这条路,我的建议是尝试使用您已经了解的 Office API,但使用 F# 语法。如果在某些时候您确实不知道如何执行特定任务,请使用您的代码在 stackoverflow 上提出有关该任务的问题,并明确您想要做的事情。与广泛的包罗万象的问题相比,这些问题的回答速度快得离谱,所以您不会等待太久。 (程序员似乎喜欢有具体答案的精确问题^^)
我希望它能有所帮助。
I don't think their is a specific F# library for Office, so you will just use the exact same .NET library that you use in VB.NET/C#. F# is a .NET language, so anything that can be done in C# can be done in F# (but you probably already knew that :) ). The API call will be exactly the same, it just that they will be done using the F# syntax instead of the VB/C# one. So for example something that look like this
Will be expressed in F# as
Now, what you will soon realize is that the API isn't exactly designed to be easily used from a functional language. It can be done, but this task in particuliar is much more tailored to C#/VB.NET.
If you really want to enjoy F#, I suggest you use in area where its strength really show. My personal experience is that functional language are awesome when a lot of math is involved. It is also marvellous if you want to easily introduce parallelism in your application (since F# code is usually side effect free). So anything that require data crunching on a lot of data is perfect for it. But for task that consist mainly of putting together a bunch of API call to an external library, F# is kind of meh. You could say that F# is kind of like a graphic card programming language, while C# a general purpose CPU programming language. A lot of thing run better with C#, but the stuff that run better on F# run really better on it.
But if you really want to go that route, my suggestion is to try to use the Office API as you already know it, but with a F# syntax. If at some point you really have no idea how to do a specific task, ask a question about it on stackoverflow with your code and exactly want you want to do. Those question get answered ridiculously fast compared to broad all-encompassing question, so you won't wait long. (Programmer seem to love precise question with a specific answer ^^)
I hope that it helped a little.
我发现了这个http://iouri-khramtsov。 blogspot.co.uk/2011/12/automating-excel-with-f.html 有用的建议。简而言之,您可以使用如下内容:
将 F# 与 Excel 结合使用似乎是很自然的选择。
在 Excel 中获取结果需要使用多个不可变值,每个值都由公式驱动。 Excel 有一个出色的用户界面,一个可爱的世界模型 - 我喜欢行、列和单元格 - 但要自动化或自定义事物需要宏。当你会使用 F# 时为什么要学习这个?公式和不可变的值是其设计的基础。
理想情况下,您也可以在 F# 中将公式自己编写为用户定义函数 (UDF) - 请参阅 http://excel-dna。网/ .然后,也许您想对对象/类型做一些有趣的事情 - 查找“github com mndrake ExcelObjectHandler”(我没有足够的声誉来发布第三个链接)。
杰克
I found this http://iouri-khramtsov.blogspot.co.uk/2011/12/automating-excel-with-f.html helpful advice. Briefly, you'd use something like this:
Using F# with Excel seems like a natural fit.
Getting to a result in Excel requires the use of several immutable values, each driven by formulas. Excel has a brilliant user interface, a lovely model of the world - I love rows, columns and cells - but to automate or customise things requires macros. Why learn this when you can use F#? Formulas and immutable values are fundamental to its design.
Ideally you'd write formulas yourself as a User Defined Function (UDFs) also in F# - see http://excel-dna.net/ . Then, perhaps, you'd want to do something interesting with objects/types - Look for "github com mndrake ExcelObjectHandler" (I don't have enough reputation to post a 3rd link).
Jack