Skip to content

Commit

Permalink
Add FsWorkbook FromXlsxStream extension
Browse files Browse the repository at this point in the history
  • Loading branch information
muehlhaus committed Mar 13, 2023
1 parent c1d54b2 commit 96f5b36
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
40 changes: 40 additions & 0 deletions src/FsSpreadsheet.ExcelIO/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ open System.IO
[<AutoOpen>]
module FsExtensions =

type FsCell with
//member self.ofXlsxCell (sst : Spreadsheet.SharedStringTable option) (xlsxCell:Spreadsheet.Cell) =
// let v = Cell.getValue sst xlsxCell
// let row,col = xlsxCell.CellReference.Value |> CellReference.toIndices
// FsCell.create (int row) (int col) v

static member ofXlsxCell (sst : Spreadsheet.SharedStringTable option) (xlsxCell:Spreadsheet.Cell) =
let v = Cell.getValue sst xlsxCell
let row,col = xlsxCell.CellReference.Value |> CellReference.toIndices
FsCell.create (int row) (int col) v

type FsTable with

/// Returns the FsTable with given FsCellsCollection in the form of an XlsxTable.
Expand Down Expand Up @@ -82,6 +93,35 @@ module FsExtensions =


type FsWorkbook with

member self.FromXlsxStream (stream : Stream) =
let doc = Spreadsheet.fromStream stream false
let sst = Spreadsheet.tryGetSharedStringTable doc
let xlsxWorkbookPart = Spreadsheet.getWorkbookPart doc
let xlsxWorkbook = Workbook.get xlsxWorkbookPart
let xlsxSheets =
Sheet.Sheets.get xlsxWorkbook
|> Sheet.Sheets.getSheets

let sheets =
xlsxSheets
|> Seq.map (fun xlsxSheet ->
let sheetIndex = Sheet.getSheetIndex xlsxSheet
let xlsxCells =
Spreadsheet.getCellsBySheetIndex sheetIndex doc
|> Seq.map (FsCell.ofXlsxCell sst)
FsWorksheet(xlsxSheet.Name)
|> FsWorksheet.addCells xlsxCells
|> (fun fsws -> fsws.RescanRows(); fsws)
)


sheets
|> Seq.fold (fun wb sheet -> FsWorkbook.addWorksheet sheet wb) (new FsWorkbook())

//member self.FromFile (path:string) =
// use sr = new StreamReader(path)
// self.FromXlsxStream sr.

/// Writes the FsWorkbook into a given MemoryStream.
member self.ToStream(stream : MemoryStream) =
Expand Down
1 change: 1 addition & 0 deletions src/FsSpreadsheet/Cells/FsCell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type DataType =
/// Creates an FsCell of `DataType` dataType, with value of type `string`, and `FsAddress` address.
type FsCell (value : IConvertible, dataType : DataType, address : FsAddress) =

// TODO: Maybe save as IConvertible
let mutable _cellValue = string value
let mutable _dataType = dataType
let mutable _comment = ""
Expand Down
17 changes: 11 additions & 6 deletions src/FsSpreadsheet/FsWorkbook.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,33 @@ type FsWorkbook() =
/// Removes a given FsWorksheet.
member self.RemoveWorksheet(sheet : FsWorksheet) =
self.RemoveWorksheet(sheet.Name)

self

// --------------
// STATIC METHODS
// --------------

/// Adds an FsWorksheet with given name to an FsWorkbook.
static member addWorksheetWithName (name : string) (workbook : FsWorkbook) =
workbook.AddWorksheet name
workbook.AddWorksheet name |> ignore
workbook

/// Adds an FsWorksheet to an FsWorkbook.
static member addWorksheet (sheet : FsWorksheet) (workbook : FsWorkbook) =
workbook.AddWorksheet sheet
workbook.AddWorksheet sheet |> ignore
workbook

/// Returns all FsWorksheets.
static member getWorksheets (workbook : FsWorkbook) =
workbook.GetWorksheets()
workbook.GetWorksheets() |> ignore
workbook

/// Removes an FsWorksheet with given name from an FsWorkbook.
static member removeWorksheetByName (name : string) (workbook : FsWorkbook) =
workbook.RemoveWorksheet name
workbook.RemoveWorksheet name |> ignore
workbook

/// Removes a given FsWorksheet from an FsWorkbook.
static member removeWorksheet (sheet : FsWorksheet) (workbook : FsWorkbook) =
workbook.RemoveWorksheet sheet
workbook.RemoveWorksheet sheet |> ignore
workbook

0 comments on commit 96f5b36

Please sign in to comment.