NPOI.FSharp


NPOI.FSharp

Documentation

The NPOI.FSharp library can be installed from NuGet:
PM> Install-Package NPOI.FSharp

Example

This (bad) example demonstrates using the library to convert an exel spreadsheet to CSV.

... don't use this - it has bugs - it's only an example of what the API looks like ;)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
#r "NPOI"
#r "NPOI.OOXML"
#r "NPOI.FSharp"

open NPOI.FSharp
open System.Globalization

let rec formatCellValueForCsv = function
  | CellValue.Number n -> n.ToString(CultureInfo.InvariantCulture)
  | CellValue.String s -> sprintf "\"%s\"" (s.Replace("\"","\"\""))
  | CellValue.Formula(formula, cachedResult) -> formula
  | CellValue.Blank -> ""
  | CellValue.Boolean b -> b.ToString(CultureInfo.InvariantCulture)
  | CellValue.Error e -> sprintf "Error: %i" e
  | CellValue.Unknown t -> "Error: Unknown cell type"

let formatCellForCsv cell =
  cell |> Cell.value |> formatCellValueForCsv

let formatRowForCsv row =
  row
  |> Row.cellsWithContent
  |> Seq.map formatCellForCsv
  |> String.concat ","

let toCsv workbook =
  workbook
  |> Workbook.activeSheet
  |> Sheet.rowsWithContent
  |> Seq.map formatRowForCsv
  |> String.concat "\n"

let xlsxToCsv filePath =
  filePath
  |> System.IO.File.ReadAllBytes
  |> Workbook.fromXlsxBytes
  |> toCsv

let xlsToCsv filePath =
  filePath
  |> System.IO.File.ReadAllBytes
  |> Workbook.fromXlsBytes
  |> toCsv

Some more info

Samples & documentation

The library comes with comprehensible documentation. It can include tutorials automatically generated from *.fsx files in the content folder. The API reference is automatically generated from Markdown comments in the library implementation.

  • Tutorial contains a further explanation of this sample library.

  • API Reference contains automatically generated documentation for all types, modules and functions in the library. This includes additional brief samples on using most of the functions.

Contributing and copyright

The project is hosted on GitHub where you can report issues, fork the project and submit pull requests. If you're adding a new public API, please also consider adding samples that can be turned into a documentation. You might also want to read the library design notes to understand how it works.

The library is available under Public Domain license, which allows modification and redistribution for both commercial and non-commercial purposes. For more information see the License file in the GitHub repository.

Fork me on GitHub