Getting Started
To start using ExcelProvider simply reference the ExcelProvider NuGet Package.
For use in a project, use the following command in the Package Manager Console:
PM> Install-Package ExcelProvider
For use in an F# script, use the following directive:
#r "nuget: ExcelProvider"
After referencing the package, open FSharp.Interop.Excel
and you will have access to the Type Provider functionality.
You can create a type for an individual workbook. The simplest option is to specify just the name of the workbook. You will then be given typed access to the data held in the first sheet. The first row of the sheet will be treated as field names and the subsequent rows will be treated as values for these fields.
Parameters
When creating the type you can specify the following parameters:
FileName
Location of the Excel file.SheetName
Name of sheet containing data. Defaults to first sheet.Range
Specification usingA1:D3
type addresses of one or more ranges. Defaults to use whole sheet.HasHeaders
Whether the range contains the names of the columns as its first line.ForceString
Specifies forcing data to be processed as strings. Defaults tofalse
.
All but the first are optional.
The parameters can be specified by position or by using the name - for example the following are equivalent:
open FSharp.Interop.Excel
type MultipleSheets1 = ExcelFile<"MultipleSheets.xlsx", "B">
type MultipleSheets2 = ExcelFile<"MultipleSheets.xlsx", SheetName="B">
Example
This example shows the use of the type provider in an F# script on a sheet containing three rows of data:
// reference the type provider
open FSharp.Interop.Excel
// Let the type provider do it's work
type DataTypesTest = ExcelFile<"DataTypes.xlsx">
let file = new DataTypesTest()
let row = file.Data |> Seq.head
let test = row.Float
And the variable test
has the following value:
|
namespace FSharp
--------------------
namespace Microsoft.FSharp
--------------------
ExcelFile<...>() : ExcelFile<...>
ExcelFile<...>(filename: string) : ExcelFile<...>
ExcelFile<...>(filename: string, sheetname: string) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat, sheetname: string) : ExcelFile<...>
<summary>Typed representation of data in an Excel file.</summary> <param name='FileName'>Location of the Excel file.</param> <param name='SheetName'>Name of sheet containing data. Defaults to first sheet.</param> <param name='Range'>Specification using `A1:D3` type addresses of one or more ranges. Defaults to use whole sheet.</param> <param name='HasHeaders'>Whether the range contains the names of the columns as its first line.</param> <param name='ForceString'>Specifies forcing data to be processed as strings. Defaults to `false`.</param>
namespace FSharp
--------------------
namespace Microsoft.FSharp
type DataTypesTest = ExcelFile<...>
--------------------
ExcelFile<...>() : ExcelFile<...>
ExcelFile<...>(filename: string) : ExcelFile<...>
ExcelFile<...>(filename: string, sheetname: string) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat, sheetname: string) : ExcelFile<...>
type ExcelFile = inherit ExcelFileInternal
<summary>Typed representation of data in an Excel file.</summary> <param name='FileName'>Location of the Excel file.</param> <param name='SheetName'>Name of sheet containing data. Defaults to first sheet.</param> <param name='Range'>Specification using `A1:D3` type addresses of one or more ranges. Defaults to use whole sheet.</param> <param name='HasHeaders'>Whether the range contains the names of the columns as its first line.</param> <param name='ForceString'>Specifies forcing data to be processed as strings. Defaults to `false`.</param>
--------------------
ExcelFile<...>() : ExcelFile<...>
ExcelFile<...>(filename: string) : ExcelFile<...>
ExcelFile<...>(filename: string, sheetname: string) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat, sheetname: string) : ExcelFile<...>
val file: DataTypesTest
--------------------
ExcelFile<...>() : ExcelFile<...>
ExcelFile<...>(filename: string) : ExcelFile<...>
ExcelFile<...>(filename: string, sheetname: string) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat, sheetname: string) : ExcelFile<...>
val row: ExcelFile<...>.Row
--------------------
ExcelFile<...>() : ExcelFile<...>
ExcelFile<...>(filename: string) : ExcelFile<...>
ExcelFile<...>(filename: string, sheetname: string) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat, sheetname: string) : ExcelFile<...>
module Seq from Microsoft.FSharp.Collections
--------------------
ExcelFile<...>() : ExcelFile<...>
ExcelFile<...>(filename: string) : ExcelFile<...>
ExcelFile<...>(filename: string, sheetname: string) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat, sheetname: string) : ExcelFile<...>
val test: float
--------------------
ExcelFile<...>() : ExcelFile<...>
ExcelFile<...>(filename: string) : ExcelFile<...>
ExcelFile<...>(filename: string, sheetname: string) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat) : ExcelFile<...>
ExcelFile<...>(stream: System.IO.Stream, format: ExcelFormat, sheetname: string) : ExcelFile<...>