Home » excel » Import Excel data into F#

Import Excel data into F#

Posted by: admin April 23, 2020 Leave a comment


I would like to import some basic data (only few integers) from an Excel file into a F# list.
Here is the code I compute which open an Excel file, take the value of the Cell “C6” and ‘store’ it in a variable called l.
However it does not compiled as an type error appeared. Indeed type of first value is obj.
How can I convert it into 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
How to&Answers:

I think just add

:?> int

to the end of the last line to downcast firstValue, but see also the linked duplicate question for more on Excel interop.


What is the real type of firstValue? If it’s an int, use a dynamic cast:

firstValue :?> int

Otherwise maybe converting to a string and then an int would work:

firstValue |> string |> int