Igor Kulman

Reading Excel sheets using F# without COM

· Igor Kulman

I needed to create an utility that would read Excel 2010 files (.xlsx) and generate XML files from them according to some specific rules. The catch was that the utility needed to run on MacOS instead of Windows.

Reading and writing Excel files from .NET is very easy using the Microsoft.Office.Interop assemblies but they use Excel through COM and that makes them unusable outside of Windows. I found some 3rd party libraries for working with Excel and analzyed them with The Mono Migration Analzyer (MoMA). MoMA is a handy tool that analyzes .NET assemblies and tells you if they will run on Mono. Many of the Excel libraries I found were unusable for my use-case because they were using PInvoke calls. Only the ExcelPackage library runs on Mono.

I wanted to use the ExcelPackage from F# not C# so I wrote a very simple F# wrapper, that you can freely use. For now it contains just a few methods

Excel.getWorksheets

Excel.getWorksheetByIndex

Excel.getWorksheetByName

Excel.getMaxRowNumber

Excel.getMaxColNumber

Excel.getContent

Excel.getColumn

Excel.getRow

but I will be adding a few more soon. The usage is really simple. For example, if you want to read the whole sheet number 1 from a file called text.xlsx, use

let data = 
        "test.xlsx"
        |> Excel.getWorksheetByIndex 1
        |> Excel.getContent 

data 
    |> Seq.iter (fun x-> printfn "%s" x)

EDIT: Now available as a Nuget package:

PM> Install-Package ExcelPackageF

See also