将Excel数据导入F#

时间:2011-08-30 17:20:00

标签: excel f#

我想将一些基本数据(只有几个整数)从Excel文件导入到F#列表中。 这是我计算的代码,它打开一个Excel文件,获取Cell“C6”的值并将其“存储”在一个名为l的变量中。 但是它没有编译为出现类型错误。实际上,第一个值的类型是obj。 如何将其转换为int?

//#r "Microsoft.Office.Interop.Excel"

//#r "office"

open Microsoft.Office.Interop


let xlApp = new Excel.ApplicationClass()

let xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\Fabien C\Desktop\algo données.xlsx")

let xlWorkSheet = xlWorkBook.Worksheets.["Produits"] :?> Excel.Worksheet

let firstValue = xlWorkSheet.Cells.[6,3]

let (l : int) = firstValue

2 个答案:

答案 0 :(得分:1)

firstValue的真实类型是什么?如果是int,请使用动态转换:

firstValue :?> int

否则可能转换为字符串,然后int将起作用:

firstValue |> string |> int

答案 1 :(得分:1)

我想添加

:?> int

到最后一行的结尾,以向下转换firstValue,但另请参阅链接的重复question以获取有关Excel互操作的更多信息。