Interop between F# and C# isn't a problem. You have various errors in your F# class:
You cast an Object
sh
to Worksheetsht
, but mistakenly usesh
instead ofsht
later.Range
doesn't haveItem
property.You add virtually everything related to Excel programming into the project. You only need
Microsoft.Office.Interop.Excel
in this example. It's better to remove unnecessaryopen
commands, especiallyMicrosoft.Office.Tools.Excel
which is opened afterInterop.Excel
could have objects of the same names but have incompatible fields/interfaces.You should pass a
Worksheet
instead of anobj
to avoid downcasting.
A minimal F# example that works:
namespace DataLib
open Microsoft.Office.Interop.Excel
type Class1() =
member this.X = "F#"
member this.Readsheet (sh: Worksheet) =
let v = sh.Cells.[1, 1]
sh.Range(1, 1).ToString()
Moreover, your C# syntax is a bit off, you need something like:
// Add "using Microsoft.Office.Interop.Excel;"
// and "using DataLib;" to namespace declarations
public void useExcel(Worksheet sh) // Worksheet is Excel Interop object
{
Class1 one = new Class1();
one.Readsheet(sh);
}