f# read xls file - how to parse a value2 object

I tried to use F# read a xls file as below

open Microsoft.Office.Interop.Excel
let app = ApplicationClass(Visible = false)
let book = app.Workbooks.Open "test.xls"
let sheet = book.Worksheets.[1] :?> _Worksheet
let vals = sheet.UsedRange.Value2

The problem is how can I parse vals into a F# type? in fsx.exe, the vals showed as

'val vals: obj = [bound1
                  bound2
                  ["colname1"; "colname2"; ...]
                  [1234,5678,]...]

I wanted to retrieve the string represetation first, but printfn "%A" vals.ToString();; shows "System.Object[,]" only. If I then try to access vals.[1,1], I got error The field,constructor or member 'item' is not defined

thanks,

Answers


The type of Value2 is obj. If the range represents just a single cell, the actual type will be some primitive type (int, float, decimal, string). If the range represents several cells (your case), then the returned value is a two-dimensional .NET array of type obj[,].

You can cast the value returned by Value2 to an array and access it using indexers:

let vals = sheet.UsedRange.Value2 :?> obj[,]
vals.[1, 1]

Note that the returned array is 1-based (and not zero based as usual). The indexer again returns obj, so you need to cast the values to their actual type. Depending on your sheet, this will be probably float or string:

let firstTitle = vals.[1, 1] :?> string
let firstValue = vals.[2, 1] :?> float

(Assuming you have a title in A1 and a number in A2)


Need Your Help

add image to button in javascript

javascript jquery windows-phone-7

I have created a button in javascript and I would like to use an image for the button instead of text, but I have not yet found a way to do this. So far the sources I have referenced are not workin...

iPhone Programming - Passing NSMutableArray between view controllers

iphone ios cocoa-touch uiviewcontroller nsmutablearray

I have a RootViewController and a DetailViewController where I am trying to pass a NSMutableArray from the rootView to the detailView. I have created an object of the RootViewController inside the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.