diff --git a/excel-importer-test/src_test/com/axonivy/util/excel/test/TestExcelReader.java b/excel-importer-test/src_test/com/axonivy/util/excel/test/TestExcelReader.java index 993c249..5d0471e 100644 --- a/excel-importer-test/src_test/com/axonivy/util/excel/test/TestExcelReader.java +++ b/excel-importer-test/src_test/com/axonivy/util/excel/test/TestExcelReader.java @@ -28,7 +28,9 @@ void parseColumns_xlsx(@TempDir Path dir) throws IOException { assertThat(columns).contains( new Column("Firstname", String.class, 255), new Column("ZIP", Integer.class), new Column("Amount", Double.class), new Column("Birthdate", Timestamp.class), // should be a date - new Column("Note", String.class, 811) + new Column("Note", String.class, 811), + new Column("Column contains texts in incorrect number format", String.class, 255), + new Column("Column contains both text and numeric", String.class, 255) ); } diff --git a/excel-importer-test/src_test/com/axonivy/util/excel/test/sample.xlsx b/excel-importer-test/src_test/com/axonivy/util/excel/test/sample.xlsx index 9d37210..891f81e 100644 Binary files a/excel-importer-test/src_test/com/axonivy/util/excel/test/sample.xlsx and b/excel-importer-test/src_test/com/axonivy/util/excel/test/sample.xlsx differ diff --git a/excel-importer/src/com/axonivy/util/excel/importer/ExcelReader.java b/excel-importer/src/com/axonivy/util/excel/importer/ExcelReader.java index 7dbd622..5c9ffd4 100644 --- a/excel-importer/src/com/axonivy/util/excel/importer/ExcelReader.java +++ b/excel-importer/src/com/axonivy/util/excel/importer/ExcelReader.java @@ -1,12 +1,14 @@ package com.axonivy.util.excel.importer; import java.sql.Timestamp; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DateUtil; @@ -87,11 +89,40 @@ private static void updateColumn(Column column, Cell cell) { && !CellUtils.isInteger(cell)) { column.setType(Double.class); } + if (cell.getCellType() == CellType.STRING) { + column.setType(String.class); + column.setDatabaseFieldLength(DEFAULT_STRING_LENGTH); + } if (column.getType().equals(String.class)) { - var cellLength = cell.getStringCellValue().length(); - if (cellLength > column.getDatabaseFieldLength()) { - column.setDatabaseFieldLength(cellLength); + var cellValue = getCellValueAsString(cell); + if (cellValue.length() > column.getDatabaseFieldLength()) { + column.setDatabaseFieldLength(cellValue.length()); + } + } + } + + public static String getCellValueAsString(Cell cell) { + if (cell == null) { + return StringUtils.EMPTY; + } + switch (cell.getCellType()) { + case STRING: + return cell.getStringCellValue(); + case NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + return cell.getDateCellValue().toString(); + } else { + DecimalFormat decimalFormat = new DecimalFormat("#"); + decimalFormat.setMaximumFractionDigits(0); + decimalFormat.setGroupingUsed(false); + return decimalFormat.format(cell.getNumericCellValue()); } + case BOOLEAN: + return String.valueOf(cell.getBooleanCellValue()); + case FORMULA: + return cell.getCellFormula(); + default: + return StringUtils.EMPTY; } } }