Skip to content

Commit 5f22e01

Browse files
author
Olivier Chédru
authored
Support for boolean cells (#56)
1 parent 01778d4 commit 5f22e01

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Cell.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void write(Writer w, int r, int c) throws IOException {
5252
w.append(" s=\"").append(style).append('\"');
5353
}
5454
if (value != null && !(value instanceof Formula)) {
55-
w.append(" t=\"").append((value instanceof CachedString) ? 's' : 'n').append('\"');
55+
w.append(" t=\"").append((value instanceof CachedString) ? 's' : (value instanceof Boolean ? 'b' : 'n')).append('\"');
5656
}
5757
w.append(">");
5858
if (value instanceof Formula) {
@@ -67,6 +67,8 @@ void write(Writer w, int r, int c) throws IOException {
6767
w.append((long) value);
6868
} else if (value instanceof Double) {
6969
w.append((double) value);
70+
} else if (value instanceof Boolean) {
71+
w.append(((Boolean) value).booleanValue() ? '1' : '0');
7072
} else {
7173
w.append(value.toString());
7274
}
@@ -81,16 +83,16 @@ void write(Writer w, int r, int c) throws IOException {
8183
*
8284
* @param wb Parent workbook.
8385
* @param v Cell value. Supported types are
84-
* {@link String}, {@link Date}, {@link LocalDate}, {@link LocalDateTime}, {@link ZonedDateTime}
85-
* and {@link Number} implementations. Note Excel timestamps do not carry
86+
* {@link String}, {@link Date}, {@link LocalDate}, {@link LocalDateTime}, {@link ZonedDateTime},
87+
* {@link Number} and {@link Boolean} implementations. Note Excel timestamps do not carry
8688
* any timezone information; {@link Date} values are converted to an Excel
8789
* serial number with the system timezone. If you need a specific timezone,
8890
* prefer passing a {@link ZonedDateTime}.
8991
*/
9092
void setValue(Workbook wb, Object v) {
9193
if (v instanceof String) {
9294
value = wb.cacheString((String) v);
93-
} else if (v == null || v instanceof Number) {
95+
} else if (v == null || v instanceof Number || v instanceof Boolean) {
9496
value = v;
9597
} else if (v instanceof Date) {
9698
value = TimestampUtil.convertDate((Date) v);

fastexcel-writer/src/main/java/org/dhatim/fastexcel/Worksheet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ public void width(int c, double width) {
314314
* @param r Zero-based row number.
315315
* @param c Zero-based column number.
316316
* @param value Cell value. Supported types are
317-
* {@link String}, {@link Date}, {@link LocalDate}, {@link LocalDateTime}, {@link ZonedDateTime}
318-
* and {@link Number} implementations. Note Excel timestamps do not carry
317+
* {@link String}, {@link Date}, {@link LocalDate}, {@link LocalDateTime}, {@link ZonedDateTime},
318+
* {@link Number} and {@link Boolean} implementations. Note Excel timestamps do not carry
319319
* any timezone information; {@link Date} values are converted to an Excel
320320
* serial number with the system timezone. If you need a specific timezone,
321321
* prefer passing a {@link ZonedDateTime}.

fastexcel-writer/src/test/java/org/dhatim/fastexcel/Correctness.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
*/
1616
package org.dhatim.fastexcel;
1717

18+
import org.apache.commons.io.output.NullOutputStream;
19+
import org.apache.poi.ss.usermodel.BorderStyle;
20+
import org.apache.poi.ss.usermodel.DataValidation.ErrorStyle;
21+
import org.apache.poi.ss.usermodel.DataValidationConstraint;
22+
import org.apache.poi.ss.usermodel.IndexedColors;
23+
import org.apache.poi.ss.usermodel.SheetVisibility;
24+
import org.apache.poi.ss.util.CellRangeAddress;
25+
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
26+
import org.apache.poi.xssf.usermodel.XSSFRow;
27+
import org.apache.poi.xssf.usermodel.XSSFSheet;
28+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
29+
import org.junit.Test;
30+
1831
import java.io.ByteArrayInputStream;
1932
import java.io.ByteArrayOutputStream;
2033
import java.io.IOException;
@@ -30,23 +43,8 @@
3043
import java.util.concurrent.ExecutionException;
3144
import java.util.function.Consumer;
3245

33-
import org.apache.commons.io.output.NullOutputStream;
34-
import org.apache.poi.ss.usermodel.BorderStyle;
35-
import org.apache.poi.ss.usermodel.DataValidation.ErrorStyle;
36-
import org.apache.poi.ss.usermodel.DataValidationConstraint;
37-
import org.apache.poi.ss.usermodel.IndexedColors;
38-
import org.apache.poi.ss.usermodel.SheetVisibility;
39-
import org.apache.poi.ss.util.CellRangeAddress;
40-
import org.apache.poi.xssf.usermodel.XSSFDataValidation;
41-
import org.apache.poi.xssf.usermodel.XSSFRow;
42-
import org.apache.poi.xssf.usermodel.XSSFSheet;
43-
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
44-
import org.junit.Test;
45-
4646
import static org.assertj.core.api.Assertions.assertThat;
47-
import static org.dhatim.fastexcel.SheetProtectionOption.DELETE_ROWS;
48-
import static org.dhatim.fastexcel.SheetProtectionOption.OBJECTS;
49-
import static org.dhatim.fastexcel.SheetProtectionOption.SHEET;
47+
import static org.dhatim.fastexcel.SheetProtectionOption.*;
5048
import static org.junit.Assert.assertFalse;
5149
import static org.junit.Assert.assertTrue;
5250

@@ -211,6 +209,8 @@ public void singleWorksheet() throws Exception {
211209
ws.value(i, i++, intValue);
212210
ws.value(i, i++, longValue);
213211
ws.value(i, i++, bigDecimalValue);
212+
ws.value(i, i++, Boolean.TRUE);
213+
ws.value(i, i++, Boolean.FALSE);
214214
try {
215215
ws.finish();
216216
} catch (IOException ex) {
@@ -240,6 +240,8 @@ public void singleWorksheet() throws Exception {
240240
assertThat(xws.getRow(i).getCell(i++).getNumericCellValue()).isEqualTo(intValue);
241241
assertThat(xws.getRow(i).getCell(i++).getNumericCellValue()).isEqualTo(longValue);
242242
assertThat(new BigDecimal(xws.getRow(i).getCell(i++).getRawValue())).isEqualTo(bigDecimalValue);
243+
assertThat(xws.getRow(i).getCell(i++).getBooleanCellValue()).isTrue();
244+
assertThat(xws.getRow(i).getCell(i++).getBooleanCellValue()).isFalse();
243245
}
244246

245247
@Test

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.dhatim</groupId>
77
<artifactId>root-oss</artifactId>
8-
<version>14.11.0</version>
8+
<version>14.11.5</version>
99
</parent>
1010

1111
<artifactId>fastexcel-parent</artifactId>

0 commit comments

Comments
 (0)