forked from xou/elixlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.exs
executable file
·50 lines (40 loc) · 2.01 KB
/
example.exs
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
44
45
46
47
48
49
50
#!/usr/bin/elixir -pa _build/dev/lib/elixlsx/ebin/
require Elixlsx
alias Elixlsx.Sheet
alias Elixlsx.Workbook
sheet1 = Sheet.with_name("First")
# Set cell B2 to the string "Hi". :)
|> Sheet.set_cell("B2", "Hi")
# Optionally, set font properties:
|> Sheet.set_cell("B3", "Hello World", bold: true, underline: true)
# Number formatting can be applied like this:
|> Sheet.set_cell("A1", 123.4, num_format: "0.00")
# Two date formats are accepted, erlang's :calendar format and UNIX timestamps.
# the datetime: true parameter automatically applies conversion to Excels internal format.
|> Sheet.set_cell("A2", {{2015, 11, 30}, {21, 20, 38}}, datetime: true)
|> Sheet.set_cell("A3", 1448882362, datetime: true)
# datetime: true ouputs date and time, yyyymmdd limits the output to just the date
|> Sheet.set_cell("A4", 1448882362, yyyymmdd: true)
# make some room in the first column, otherwise the date will only show up as ###
|> Sheet.set_col_width("A", 18.0)
workbook = %Workbook{sheets: [sheet1]}
# it is also possible to add a custom "created" date to workbook, otherwise,
# the current date is used.
workbook = %Workbook{workbook | datetime: "2015-12-01T13:40:59Z"}
# It is also possible to create a sheet as a list of rows:
sheet2 = %Sheet{name: 'Third', rows: [[1,2,3,4,5],
[1,2],
["hello", "world"]]}
workbook = Workbook.append_sheet(workbook, sheet2)
# For the list of rows approach, cells with properties can be encoded by using a
# list with the value at the head and the properties in the tail:
sheet3 = %Sheet{name: "Second", rows:
[[1,2,3],
[4,5,6, ["goat", bold: true]],
[["Bold", bold: true], ["Italic", italic: true], ["Underline", underline: true], ["Strike!", strike: true],
["Large", size: 22]],
# Unicode should work as well:
[["Müłti", bold: true, italic: true, underline: true, strike: true]]
]}
Workbook.insert_sheet(workbook, sheet3, 1)
|> Elixlsx.write_to("empty.xlsx")