Skip to content

Commit 40767f0

Browse files
committed
Gracefully handle null values in cells
1 parent ca9727e commit 40767f0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/main/groovy/com/jameskleeh/excel/internal/CreatesCells.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ abstract class CreatesCells {
187187

188188
XSSFCell cell = nextCell()
189189
setStyle(value, cell, style)
190+
if (value == null) {
191+
return
192+
}
190193
Closure callable = Excel.getRenderer(value.class)
191194
if (callable != null) {
192195
cell.setCellValue((String)callable.call(value))

src/test/groovy/com/jameskleeh/excel/RowSpec.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jameskleeh.excel
22

33
import org.apache.poi.ss.usermodel.Cell
4+
import org.apache.poi.ss.usermodel.CellType
45
import org.apache.poi.xssf.usermodel.XSSFSheet
56
import org.apache.poi.xssf.usermodel.XSSFWorkbook
67
import spock.lang.Specification
@@ -136,4 +137,22 @@ class RowSpec extends Specification {
136137
cells.next().stringCellValue == 'foox'
137138
cells.next().numericCellValue == new Double(1)
138139
}
140+
141+
void "test cell with null value"() {
142+
given:
143+
XSSFWorkbook workbook = ExcelBuilder.build {
144+
sheet {
145+
row {
146+
cell(null)
147+
}
148+
}
149+
}
150+
151+
when:
152+
Cell cell = workbook.getSheetAt(0).getRow(0).getCell(0)
153+
154+
then:
155+
noExceptionThrown()
156+
cell.getCellTypeEnum() == CellType.BLANK
157+
}
139158
}

0 commit comments

Comments
 (0)