Skip to content

Instantly share code, notes, and snippets.

@qrilka
Forked from deemp/example-xlsx.hs
Created December 12, 2022 17:45
Show Gist options
  • Save qrilka/ffd9fe984e3a50fa1b0bb6c5bcd7859c to your computer and use it in GitHub Desktop.
Save qrilka/ffd9fe984e3a50fa1b0bb6c5bcd7859c to your computer and use it in GitHub Desktop.

Revisions

  1. qrilka revised this gist Dec 12, 2022. 1 changed file with 26 additions and 11 deletions.
    37 changes: 26 additions & 11 deletions example-xlsx.hs
    Original file line number Diff line number Diff line change
    @@ -1,33 +1,48 @@
    example :: IO ()
    example = do
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE OverloadedStrings #-}
    import Codec.Xlsx as X
    import qualified Data.ByteString.Lazy as L
    import Control.Lens
    import Data.Time.Clock.POSIX

    main :: IO ()
    main = do
    ct <- getPOSIXTime
    let
    name = "3"
    ws =
    X.def
    & X.atCell (3, 2) ?~ X.Cell (Just 0) (Just $ X.CellDouble 42.0) Nothing Nothing
    & X.atCell (3, 2) ?~ X.Cell (Just 1) (Just $ X.CellDouble 142.4) Nothing Nothing
    & X.atCell (3, 3) ?~ X.Cell (Just 2) (Just $ X.CellDouble 242.5) Nothing Nothing
    & X.atCell (3, 4) ?~ X.Cell (Just 3) (Just $ X.CellDouble 342.6) Nothing Nothing
    styleSheet = sheet
    where
    -- fill
    red_ = X.def & X.colorARGB ?~ "ABABAB"
    red_ = X.def & X.colorARGB ?~ "FFFF0000"
    fill_ =
    X.def
    & X.fillPattern
    ?~ ( X.def
    & X.fillPatternBgColor ?~ red_
    & X.fillPatternFgColor ?~ red_
    & X.fillPatternType ?~ X.PatternTypeSolid
    )

    (cellXF :: X.CellXf) =
    (cellXF1 :: X.CellXf) =
    X.def
    & X.cellXfFillId ?~ 0
    & X.cellXfFillId ?~ 1
    & X.cellXfApplyFill ?~ True
    (cellXF2 :: X.CellXf) =
    X.def
    & X.cellXfFillId ?~ 2
    & X.cellXfApplyFill ?~ True
    (cellXF3 :: X.CellXf) =
    X.def
    & X.cellXfFillId ?~ 3
    & X.cellXfApplyFill ?~ True
    & X.cellXfId ?~ 0

    sheet =
    X.minimalStyleSheet
    & X.styleSheetCellXfs .~ [cellXF]
    & X.styleSheetFills .~ [fill_]
    & styleSheetCellXfs %~ (++ [cellXF1, cellXF2,cellXF3])
    & X.styleSheetFills %~ (++ [fill_, fill_])
    xlsx =
    X.def
    & X.atSheet "List1" ?~ ws
  2. @deemp deemp revised this gist Dec 12, 2022. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions example-xlsx.hs
    Original file line number Diff line number Diff line change
    @@ -5,17 +5,24 @@ example = do
    name = "3"
    ws =
    X.def
    & X.atCell (1, 3) ?~ X.Cell (Just 0) (Just $ X.CellDouble 42.0) Nothing Nothing
    & X.atCell (3, 2) ?~ X.Cell (Just 0) (Just $ X.CellDouble 42.0) Nothing Nothing
    styleSheet = sheet
    where
    -- fill
    red_ = X.def & X.colorARGB ?~ "FFFF0000"
    fill_ = X.def & X.fillPattern ?~ (X.def & X.fillPatternBgColor ?~ red_)
    red_ = X.def & X.colorARGB ?~ "ABABAB"
    fill_ =
    X.def
    & X.fillPattern
    ?~ ( X.def
    & X.fillPatternBgColor ?~ red_
    & X.fillPatternType ?~ X.PatternTypeSolid
    )

    (cellXF :: X.CellXf) =
    X.def
    & X.cellXfFillId ?~ 0
    & X.cellXfApplyFill ?~ True
    & X.cellXfId ?~ 0

    sheet =
    X.minimalStyleSheet
  3. @deemp deemp created this gist Dec 11, 2022.
    28 changes: 28 additions & 0 deletions example-xlsx.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    example :: IO ()
    example = do
    ct <- getPOSIXTime
    let
    name = "3"
    ws =
    X.def
    & X.atCell (1, 3) ?~ X.Cell (Just 0) (Just $ X.CellDouble 42.0) Nothing Nothing
    styleSheet = sheet
    where
    -- fill
    red_ = X.def & X.colorARGB ?~ "FFFF0000"
    fill_ = X.def & X.fillPattern ?~ (X.def & X.fillPatternBgColor ?~ red_)

    (cellXF :: X.CellXf) =
    X.def
    & X.cellXfFillId ?~ 0
    & X.cellXfApplyFill ?~ True

    sheet =
    X.minimalStyleSheet
    & X.styleSheetCellXfs .~ [cellXF]
    & X.styleSheetFills .~ [fill_]
    xlsx =
    X.def
    & X.atSheet "List1" ?~ ws
    & X.xlStyles .~ X.renderStyleSheet styleSheet
    L.writeFile ("example-" <> name <> ".xlsx") $ X.fromXlsx ct xlsx