有谁能够提供一个网站链接或者演示如何在.NET 4.0 (Visual Studio 2010 Beta 1)中使用F#和Excel集成呢?
我知道如何在CTP版本中实现,但据我所知,在.NET 4.0 (Visual Studio 2010 Beta 1)中应该更加简单。
#r "Microsoft.Office.Interop.Excel"
open System
open System.IO
open System.Reflection
open Microsoft.Office.Interop.Excel
let app = ApplicationClass(Visible = true)
let sheet = app.Workbooks
.Add()
.Worksheets.[1] :?> _Worksheet
let setCellText (x : int) (y : int) (text : string) =
let range = sprintf "%c%d" (char (x + int 'A')) (y+1)
sheet.Range(range).Value(Missing.Value) <- text
let printCsvToExcel rowIdx (csvText : string) =
csvText.Split([| ',' |])
|> Array.iteri (fun partIdx partText -> setCellText partIdx rowIdx partText)
let rec filesUnderFolder basePath =
seq {
yield! Directory.GetFiles(basePath)
for subFolder in Directory.GetDirectories(basePath) do
yield! filesUnderFolder subFolder
}
// Print header
printCsvToExcel 0 "Directory, Filename, Size, Creation Time"
// Print rows
filesUnderFolder (Environment.GetFolderPath(Environment.SpecialFolder.MyPictures))
|> Seq.map (fun filename -> new FileInfo(filename))
|> Seq.map (fun fileInfo -> sprintf "%s, %s, %d, %s"
fileInfo.DirectoryName
fileInfo.Name
fileInfo.Length
(fileInfo.CreationTime.ToShortDateString()))
|> Seq.iteri (fun idx str -> printCsvToExcel (idx + 1) str)
只需编写一个空的 VSTO shell,然后创建 F# 程序集 DLL 并使用它。我总是使用出色的 VS2010 设计工具,然后将其链接到 F# DLL 中,这样您就可以充分发挥两者的优势...