Skip to content

Commit a4548d3

Browse files
committed
Enable output by column. Add license headers
1 parent 026219c commit a4548d3

File tree

13 files changed

+531
-106
lines changed

13 files changed

+531
-106
lines changed

src/main/groovy/com/jameskleeh/excel/CellStyleBuilder.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
119
package com.jameskleeh.excel
220

321
import groovy.transform.CompileStatic
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
package com.jameskleeh.excel
20+
21+
import com.jameskleeh.excel.internal.CreatesCells
22+
import groovy.transform.CompileStatic
23+
import org.apache.poi.ss.util.CellRangeAddress
24+
import org.apache.poi.xssf.usermodel.XSSFCell
25+
import org.apache.poi.xssf.usermodel.XSSFRow
26+
import org.apache.poi.xssf.usermodel.XSSFSheet
27+
import org.apache.poi.xssf.usermodel.XSSFWorkbook
28+
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy
29+
30+
@CompileStatic
31+
class Column extends CreatesCells {
32+
33+
private int columnIdx
34+
private int rowIdx
35+
36+
Column(XSSFWorkbook workbook, XSSFSheet sheet, Map defaultOptions, Map<Object, Integer> columnIndexes, CellStyleBuilder styleBuilder, int columnIdx, int rowIdx) {
37+
super(workbook, sheet, defaultOptions, columnIndexes, styleBuilder)
38+
this.columnIdx = columnIdx
39+
this.rowIdx = rowIdx
40+
}
41+
42+
@Override
43+
protected XSSFCell nextCell() {
44+
XSSFRow row = sheet.getRow(rowIdx)
45+
if (row == null) {
46+
row = sheet.createRow(rowIdx)
47+
}
48+
XSSFCell cell = row.getCell(columnIdx, MissingCellPolicy.CREATE_NULL_AS_BLANK)
49+
rowIdx++
50+
cell
51+
}
52+
53+
void skipCells(int num) {
54+
rowIdx += num
55+
}
56+
57+
@Override
58+
void merge(Map style, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Column) Closure callable) {
59+
Map existingDefaultOptions = defaultOptions
60+
61+
if (style != null && !style.isEmpty()) {
62+
Map newDefaultOptions = new LinkedHashMap(style)
63+
styleBuilder.convertSimpleOptions(newDefaultOptions)
64+
newDefaultOptions = styleBuilder.merge(defaultOptions, newDefaultOptions)
65+
defaultOptions = newDefaultOptions
66+
}
67+
68+
callable.resolveStrategy = Closure.DELEGATE_FIRST
69+
callable.delegate = this
70+
int startingRowIndex = rowIdx
71+
callable.call()
72+
int endingRowIndex = rowIdx - 1
73+
if (endingRowIndex > startingRowIndex) {
74+
CellRangeAddress range = new CellRangeAddress(startingRowIndex, endingRowIndex, columnIdx, columnIdx)
75+
sheet.addMergedRegion(range)
76+
}
77+
78+
defaultOptions = existingDefaultOptions
79+
}
80+
81+
@Override
82+
void merge(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Column) Closure callable) {
83+
merge(null, callable)
84+
}
85+
}

src/main/groovy/com/jameskleeh/excel/Excel.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
119
package com.jameskleeh.excel
220

321
import groovy.transform.CompileStatic

src/main/groovy/com/jameskleeh/excel/ExcelBuilder.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
119
package com.jameskleeh.excel
220

321
import groovy.transform.CompileStatic

src/main/groovy/com/jameskleeh/excel/Font.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
119
package com.jameskleeh.excel
220

321
/**

src/main/groovy/com/jameskleeh/excel/Formula.groovy

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
119
package com.jameskleeh.excel
220

321
import groovy.transform.CompileStatic

src/main/groovy/com/jameskleeh/excel/Row.groovy

Lines changed: 29 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
119
package com.jameskleeh.excel
220

21+
import com.jameskleeh.excel.internal.CreatesCells
322
import groovy.transform.CompileStatic
423
import org.apache.poi.ss.util.CellRangeAddress
524
import org.apache.poi.xssf.usermodel.XSSFCell
@@ -11,36 +30,26 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook
1130
* A class used to create a row in an excel document
1231
*/
1332
@CompileStatic
14-
class Row {
33+
class Row extends CreatesCells {
1534

1635
private final XSSFRow row
17-
private final XSSFWorkbook workbook
18-
private final XSSFSheet sheet
19-
private Map defaultOptions
20-
private final Map<Object, Integer> columnIndexes
21-
private final CellStyleBuilder styleBuilder
36+
2237
private int cellIdx
2338

24-
Row(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row, Map defaultOptions, Map<Object, Integer> columnIndexes) {
25-
this.workbook = workbook
26-
this.sheet = sheet
39+
Row(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row, Map defaultOptions, Map<Object, Integer> columnIndexes, CellStyleBuilder styleBuilder) {
40+
super(workbook, sheet, defaultOptions, columnIndexes, styleBuilder)
2741
this.row = row
2842
this.cellIdx = 0
29-
this.defaultOptions = defaultOptions
30-
this.columnIndexes = columnIndexes
31-
this.styleBuilder = new CellStyleBuilder(workbook)
3243
}
3344

34-
private XSSFCell nextCell() {
45+
@Override
46+
protected XSSFCell nextCell() {
3547
XSSFCell cell = row.createCell(cellIdx)
3648
cellIdx++
3749
cell
3850
}
3951

40-
private void setStyle(Object value, XSSFCell cell, Map options) {
41-
styleBuilder.setStyle(value, cell, options, defaultOptions)
42-
}
43-
52+
@Override
4453
void skipCells(int num) {
4554
cellIdx += num
4655
}
@@ -53,91 +62,7 @@ class Row {
5362
}
5463
}
5564

56-
void defaultStyle(Map options) {
57-
options = new LinkedHashMap(options)
58-
styleBuilder.convertSimpleOptions(options)
59-
if (defaultOptions == null) {
60-
defaultOptions = options
61-
} else {
62-
defaultOptions = styleBuilder.merge(defaultOptions, options)
63-
}
64-
}
65-
66-
XSSFCell column(String value, Object id, final Map options = [:]) {
67-
XSSFCell cell = nextCell()
68-
cell.setCellValue(value)
69-
setStyle(value, cell, options)
70-
columnIndexes[id] = cell.columnIndex
71-
cell
72-
}
73-
74-
XSSFCell formula(String formulaString, final Map style) {
75-
XSSFCell cell = nextCell()
76-
if (formulaString.startsWith('=')) {
77-
formulaString = formulaString[1..-1]
78-
}
79-
cell.setCellFormula(formulaString)
80-
setStyle(null, cell, style)
81-
cell
82-
}
83-
84-
XSSFCell formula(String formulaString) {
85-
formula(formulaString, null)
86-
}
87-
88-
XSSFCell formula(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Formula) Closure callable) {
89-
formula(null, callable)
90-
}
91-
92-
XSSFCell formula(final Map style, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Formula) Closure callable) {
93-
XSSFCell cell = nextCell()
94-
callable.resolveStrategy = Closure.DELEGATE_FIRST
95-
callable.delegate = new Formula(cell, columnIndexes)
96-
String formula
97-
if (callable.maximumNumberOfParameters == 1) {
98-
formula = (String)callable.call(cell)
99-
} else {
100-
formula = (String)callable.call()
101-
}
102-
if (formula.startsWith('=')) {
103-
formula = formula[1..-1]
104-
}
105-
cell.setCellFormula(formula)
106-
setStyle(null, cell, style)
107-
cell
108-
}
109-
110-
XSSFCell cell() {
111-
cell('')
112-
}
113-
XSSFCell cell(Object value) {
114-
cell(value, null)
115-
}
116-
XSSFCell cell(Object value, final Map style) {
117-
118-
XSSFCell cell = nextCell()
119-
setStyle(value, cell, style)
120-
if (value instanceof String) {
121-
cell.setCellValue(value)
122-
} else if (value instanceof Calendar) {
123-
cell.setCellValue(value)
124-
} else if (value instanceof Date) {
125-
cell.setCellValue(value)
126-
} else if (value instanceof Number) {
127-
cell.setCellValue(value.doubleValue())
128-
} else if (value instanceof Boolean) {
129-
cell.setCellValue(value)
130-
} else {
131-
Closure callable = Excel.getRenderer(value.class)
132-
if (callable != null) {
133-
cell.setCellValue((String)callable.call(value))
134-
} else {
135-
cell.setCellValue(value.toString())
136-
}
137-
}
138-
cell
139-
}
140-
65+
@Override
14166
void merge(final Map style, @DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Row) Closure callable) {
14267
Map existingDefaultOptions = defaultOptions
14368

@@ -161,10 +86,12 @@ class Row {
16186
defaultOptions = existingDefaultOptions
16287
}
16388

89+
@Override
16490
void merge(@DelegatesTo(strategy = Closure.DELEGATE_FIRST, value = Row) Closure callable) {
16591
merge(null, callable)
16692
}
16793

94+
@Override
16895
void merge(Object value, Integer count, final Map style = null) {
16996
merge(style) {
17097
cell(value)

0 commit comments

Comments
 (0)