Skip to content

Commit

Permalink
add csv writing capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Feb 23, 2023
1 parent fa28800 commit bfd58ae
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 2 deletions.
18 changes: 16 additions & 2 deletions FsSpreadsheet.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30907.101
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 15.0.26124.0
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "FsSpreadsheet", "src\FsSpreadsheet\FsSpreadsheet.fsproj", "{601DB071-B467-4237-9403-E63616038B61}"
EndProject
Expand All @@ -18,6 +18,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution", "Solution", "{27
RELEASE_NOTES.md = RELEASE_NOTES.md
EndProjectSection
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FsSpreadsheet.CsvIO", "src\FsSpreadsheet.CsvIO\FsSpreadsheet.CsvIO.fsproj", "{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -64,6 +66,18 @@ Global
{CBA2E524-B861-44FE-A0B0-668E57FF2A24}.Release|x64.Build.0 = Release|Any CPU
{CBA2E524-B861-44FE-A0B0-668E57FF2A24}.Release|x86.ActiveCfg = Release|Any CPU
{CBA2E524-B861-44FE-A0B0-668E57FF2A24}.Release|x86.Build.0 = Release|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Debug|x64.ActiveCfg = Debug|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Debug|x64.Build.0 = Debug|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Debug|x86.ActiveCfg = Debug|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Debug|x86.Build.0 = Debug|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Release|Any CPU.Build.0 = Release|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Release|x64.ActiveCfg = Release|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Release|x64.Build.0 = Release|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Release|x86.ActiveCfg = Release|Any CPU
{A89CB3BC-7C15-4A71-863C-2E1DDEDDD12A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
112 changes: 112 additions & 0 deletions src/FsSpreadsheet.CsvIO/FsExtension.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
namespace FsSpreadsheet.CsvIO

open FsSpreadsheet

[<AutoOpen>]
module FsExtensions =

type FsRow with

member self.ToSparseRow(cellCollection : FsCellsCollection) : seq<string option> =

let maxColIndex = self.RangeAddress.LastAddress.ColumnNumber
let cells = self.Cells(cellCollection)
seq {
for i = 1 to maxColIndex do
cells
|> Seq.tryPick (fun cell ->
if cell.WorksheetColumn = i then
Option.Some cell.Value
else
None
)
}

type FsWorksheet with

member self.ToSparseTable() : seq<seq<string option> option>=
self.RescanRows()
self.SortRows()
let rows = self.GetRows()
let maxRowIndex = self.CellCollection.MaxRowNumber
seq {
for i = 1 to maxRowIndex do
rows
|> Seq.tryPick (fun row ->
if row.Index = i then
Some (row.ToSparseRow(self.CellCollection))
else
None
)
}

member self.ToTableString(separator : char option) : string =
let separator = separator |> Option.defaultValue ','
self.ToSparseTable()
|> Seq.map (fun row ->
match row with
| None -> ""
| Some s when Seq.isEmpty s -> ""
| Some row ->
row
|> Seq.map (fun cell -> cell |> Option.defaultValue "")
|> Seq.reduce (fun rowString cellString -> $"{rowString}{separator}{cellString}")
)
|> Seq.reduce (fun tableString rowString ->
$"{tableString}\n{rowString}"
)

type FsWorkbook with

member self.ToStream(stream : System.IO.MemoryStream,?Separator : char) =
let streamWriter = new System.IO.StreamWriter(stream)
self.GetWorksheets().Head.ToTableString(Separator)
|> streamWriter.Write
streamWriter.Flush()

member self.ToBytes(?Separator : char) =
use memoryStream = new System.IO.MemoryStream()
match Separator with
| Some s -> self.ToStream(memoryStream,s)
| None -> self.ToStream(memoryStream)
memoryStream.ToArray()

member self.ToFile(path,?Separator : char) =
match Separator with
| Some s -> self.ToBytes(s)
| None -> self.ToBytes()
|> fun bytes -> System.IO.File.WriteAllBytes (path, bytes)

static member toStream(stream : System.IO.MemoryStream,workbook : FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToStream(stream,s)
| None -> workbook.ToStream(stream)
workbook.ToStream(stream)

static member toBytes(workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToBytes(s)
| None -> workbook.ToBytes()

static member toFile(path,workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToFile(path,s)
| None -> workbook.ToFile(path)

type Writer =

static member toStream(stream : System.IO.MemoryStream,workbook : FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToStream(stream,s)
| None -> workbook.ToStream(stream)
workbook.ToStream(stream)

static member toBytes(workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToBytes(s)
| None -> workbook.ToBytes()

static member toFile(path,workbook: FsWorkbook,?Separator : char) =
match Separator with
| Some s -> workbook.ToFile(path,s)
| None -> workbook.ToFile(path)
16 changes: 16 additions & 0 deletions src/FsSpreadsheet.CsvIO/FsSpreadsheet.CsvIO.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<Compile Include="FsExtension.fs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FsSpreadsheet\FsSpreadsheet.fsproj" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/FsSpreadsheet.ExcelIO/FsExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,14 @@ module FsExtensions =

static member toFile(path,workbook: FsWorkbook) =
workbook.ToFile(path)

type Writer =

static member toStream(stream : System.IO.MemoryStream,workbook : FsWorkbook) =
workbook.ToStream(stream)

static member toBytes(workbook: FsWorkbook) =
workbook.ToBytes()

static member toFile(path,workbook: FsWorkbook) =
workbook.ToFile(path)

0 comments on commit bfd58ae

Please sign in to comment.