Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MARP-637: Cells with "wrong" numbers should not be reconized as numbers #59

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -87,11 +89,41 @@ private static void updateColumn(Column column, Cell cell) {
&& !CellUtils.isInteger(cell)) {
column.setType(Double.class);
}
if (cell.getCellType() == CellType.STRING) {
column.setType(String.class);
linhpd-axonivy marked this conversation as resolved.
Show resolved Hide resolved
}
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() == null ? 0 : column.getDatabaseFieldLength())) {
linhpd-axonivy marked this conversation as resolved.
Show resolved Hide resolved
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();
case BLANK:
return "";
linhpd-axonivy marked this conversation as resolved.
Show resolved Hide resolved
default:
return "Unsupported cell type";
linhpd-axonivy marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading