将Excel数据导入到F#
我想从Excel文件导入一些基本的数据(只有less数整数)到F#列表中。 这里是我计算的代码打开一个Excel文件,把单元格的值“C6”和“存储”在一个名为l的variables。 但是,它不会编译为出现types错误。 事实上,第一个值的types是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
我想只是补充
:?> int
到倒数第一个值的最后一行的结尾,但也可以参见Excel interop上更多的链接重复问题 。
firstValue
的真实types是firstValue
? 如果它是一个int,则使用dynamic强制转换:
firstValue :?> int
否则,可能转换为一个string,然后一个int将工作:
firstValue |> string |> int