0的F#和Excel集成

0的F#和Excel集成

本文介绍了.NET 4.0的F#和Excel集成(Visual Studio 2010 Beta 1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在这里使用Web链接或演示如何使用.NET 4.0(Visual Studio 2010 Beta 1)进行F#和Excel集成?

Can anyone give a web link to or demonstrate here how to do F# and Excel integration using .NET 4.0 (Visual Studio 2010 Beta 1)?

我知道如何在CTP发行版中这样做,但据我所知,.NET 4.0(Visual Studio 2010 Beta 1)应该更简单。

I know how to do this in the CTP release but as far as I know it should be simpler in .NET 4.0 (Visual Studio 2010 Beta 1).

推荐答案

在最新的F#CTP版本(Visual Studio 2010 Beta1)中添加了secret sauce,以改善Office interop。也许你有F#与 C#对Dynamic的新支持

There is no 'secret sauce' added to the latest F# CTP release (Visual Studio 2010 Beta1) to improve Office interop. Perhaps you have F# confused with C#'s new support for Dynamic.

但是,F#中的Office interop与C#相同 - 您可以使用本机COM API或更新的受管理的Visual Studio Tools for Office(VSTO)库。不幸的是,F#没有用于创建C#的VSTO加载项的UI设计器,所以做最简单的办法是使用COM API。

However, Office interop in F# is the same as C# - you can use the native COM APIs or the newer, managed Visual Studio Tools for Office (VSTO) libraries. Unfortunately F# doesn't have the UI-designers for creating VSTO add-ins like C#, so the simplest way to do Office interop is to use the COM APIs.

以下代码片段创建一个包含我的图片文件夹中图片信息的Excel工作表:

The following snippet creates an Excel worksheet with information about the pictures in your My Pictures folder:

#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)

这篇关于.NET 4.0的F#和Excel集成(Visual Studio 2010 Beta 1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:41