You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This module supports setting basic and custom types to cell values. The following types are supported out of the box:
7
+
8
+
* String
9
+
* Calendar
10
+
* Date
11
+
* Double
12
+
* Boolean
13
+
14
+
Support for additional types can be easily added without the need to convert your custom objects each time.
15
+
16
+
=== Custom Renderers
17
+
18
+
You can register a custom cell renderer to marshal your custom type into a type that is supported by Excel.
19
+
20
+
[source,groovy]
21
+
.MyCustomClass.groovy
22
+
----
23
+
class MyCustomClass {
24
+
String name
25
+
}
26
+
----
27
+
28
+
[source,groovy]
29
+
----
30
+
import com.jameskleeh.excel.Excel
31
+
32
+
//Register a renderer for your class
33
+
Excel.registerCellRenderer(MyCustomClass) { MyCustomClass c ->
34
+
c.name
35
+
}
36
+
----
37
+
38
+
Now when you are making calls to `cell()`, you can pass your entire `MyCustomClass` instance as the value and it will be converted to just use the name.
39
+
40
+
[source,groovy]
41
+
----
42
+
def c = new MyCustomClass(name: "Sally")
43
+
44
+
ExcelBuilder.build {
45
+
sheet {
46
+
row(c) //The value "Sally" will be put into cell A1
This module exposes a Groovy DSL to create real Excel documents using Apache POI under the hood. The goal of this module is to abstract away the most common uses of Apache POI to make creating Excel documents very easy, while providing a hook into the underlying API to allow users to do anything that is not provided automatically.
In addition to creating cells, there are also a few other methods available to you to make documents easier
5
+
6
+
=== Defaults
7
+
8
+
You can apply default styling at the row level in addition to the sheet level. Styles that conflict with defaults at the sheet level will override.
9
+
10
+
[source,groovy]
11
+
----
12
+
ExcelBuilder.build {
13
+
sheet {
14
+
defaultStyle [:]
15
+
row {
16
+
defaultStyle [:]
17
+
}
18
+
}
19
+
}
20
+
----
21
+
22
+
=== Skipping Cells
23
+
24
+
[source,groovy]
25
+
----
26
+
ExcelBuilder.build {
27
+
sheet {
28
+
row {
29
+
cell("foo")
30
+
skipCells(2)
31
+
cell("bar")
32
+
}
33
+
}
34
+
}
35
+
----
36
+
37
+
The previous example will produce a document with "foo" in cell A1 and "bar" in D1
38
+
39
+
==== Skipping to a column
40
+
41
+
42
+
[source,groovy]
43
+
----
44
+
ExcelBuilder.build {
45
+
sheet {
46
+
columns {
47
+
column("Foo", "foo")
48
+
skipCells(1)
49
+
column("Bar", "bar")
50
+
}
51
+
row {
52
+
skipTo("bar")
53
+
cell("C2")
54
+
}
55
+
}
56
+
}
57
+
----
58
+
59
+
The example above will put the text "C2" in cell C2.
60
+
61
+
WARNING: The `skipTo` method will set the cell index so the next cell call will output data into the desired cell. Subsequent cells will start from that index. View the example below
62
+
63
+
[source,groovy]
64
+
----
65
+
ExcelBuilder.build {
66
+
sheet {
67
+
columns {
68
+
column("Foo", "foo")
69
+
skipCells(1)
70
+
column("Bar", "bar")
71
+
}
72
+
row {
73
+
cell("A2")
74
+
cell("B2")
75
+
skipTo("foo")
76
+
cell("replaces A2")
77
+
}
78
+
}
79
+
}
80
+
----
81
+
82
+
The first call to `cell` sets the value to "A2". The `skipTo` call sets the index so the next call will set the cell value to "replaces A2".
0 commit comments