Skip to content

Commit bf4d8d3

Browse files
committed
Changed the styling of numeric values and dates in the writeValue() method.
1 parent 3d44e0e commit bf4d8d3

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/main/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelCell.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,19 @@ public void writeValue(Object val) {
162162
if (val == null) {
163163
this.cell.setCellValue("");
164164
} else if (val instanceof Integer || val instanceof Long) {
165-
this.formatStyle((short) 1);
165+
this.formatStyle("0");
166166
this.cell.setCellValue(Integer.parseInt(String.valueOf(val)));
167167
} else if (val instanceof Double) {
168-
this.formatStyle((short) 4);
168+
this.formatStyle("#0.00");
169169
this.cell.setCellValue(Double.parseDouble(String.valueOf(val)));
170170
} else if (val instanceof Date) {
171-
this.formatStyle((short) 22);
171+
this.formatStyle("yyyy-MM-dd HH:mm");
172172
this.cell.setCellValue((Date) val);
173173
} else if (val instanceof LocalDate) {
174-
this.formatStyle((short) 14);
174+
this.formatStyle("yyyy-MM-dd");
175175
this.cell.setCellValue((LocalDate) val);
176176
} else if (val instanceof LocalDateTime) {
177-
this.formatStyle((short) 22);
177+
this.formatStyle("yyyy-MM-dd HH:mm");
178178
this.cell.setCellValue((LocalDateTime) val);
179179
} else if (val instanceof Boolean) {
180180
cell.setCellValue((Boolean) val);
@@ -205,4 +205,18 @@ public void formatStyle(short dataFormat) {
205205
newCellStyle.setDataFormat(dataFormat);
206206
this.cell.setCellStyle(newCellStyle);
207207
}
208+
209+
/**
210+
* Format the cell according to the chosen format
211+
* @param dataFormat The string that defines the data format
212+
* @since 0.5.0
213+
*/
214+
public void formatStyle(String dataFormat) {
215+
ExcelWorkbook excelWorkbook = this.getRow().getSheet().getWorkbook();
216+
CreationHelper creationHelper = excelWorkbook.getWorkbook().getCreationHelper();
217+
CellStyle newCellStyle = excelWorkbook.getWorkbook().createCellStyle();
218+
newCellStyle.cloneStyleFrom(this.cell.getCellStyle());
219+
newCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat(dataFormat));
220+
this.cell.setCellStyle(newCellStyle);
221+
}
208222
}

src/test/java/io/github/mbenincasa/javaexcelutils/model/excel/ExcelCellTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void readValueAsString() throws OpenWorkbookException, ExtensionNotValidExceptio
109109
excelCell3.writeValue(false);
110110
Assertions.assertEquals("Text", excelCell.readValueAsString());
111111
Assertions.assertEquals("21", excelCell1.readValueAsString());
112-
Assertions.assertEquals("1/1/21 21:21", excelCell2.readValueAsString());
112+
Assertions.assertEquals("2021-01-01 21:21", excelCell2.readValueAsString());
113113
Assertions.assertEquals("FALSE", excelCell3.readValueAsString());
114114
}
115115

0 commit comments

Comments
 (0)