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

Be tolerant to cell formatter problems #969

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
35 changes: 28 additions & 7 deletions excel/src/main/java/tech/tablesaw/io/xlsx/XlsxReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.IllegalFormatException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -46,6 +47,8 @@
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.tablesaw.api.ColumnType;
import tech.tablesaw.api.DoubleColumn;
import tech.tablesaw.api.LongColumn;
Expand All @@ -57,7 +60,7 @@

@Immutable
public class XlsxReader implements DataReader<XlsxReadOptions> {

private static final Logger logger = LoggerFactory.getLogger(XlsxReader.class);
private static final XlsxReader INSTANCE = new XlsxReader();

static {
Expand Down Expand Up @@ -377,10 +380,18 @@ private Column<?> appendValue(Column<?> column, Cell cell) {
Column<String> stringColumn = (Column<String>) column;
String dataFormatStyle = cell.getCellStyle().getDataFormatString();
String val;
if ("general".equalsIgnoreCase(dataFormatStyle)) {
val = new CellGeneralFormatter().format(cell.getNumericCellValue());
} else {
val = new CellNumberFormatter(dataFormatStyle).format(cell.getNumericCellValue());
try {
if ("general".equalsIgnoreCase(dataFormatStyle)) {
val = new CellGeneralFormatter().format(cell.getNumericCellValue());
} else {
val = new CellNumberFormatter(dataFormatStyle).format(cell.getNumericCellValue());
}
} catch (IllegalFormatException e) {
logger.warn(
"Error formatting cell value {} to string: {}. Ignoring it's value",
cell.getNumericCellValue(),
e.getMessage());
val = null;
}
stringColumn.append(val);
}
Expand All @@ -395,8 +406,18 @@ private Column<?> appendValue(Column<?> column, Cell cell) {
// If column has String type try to honor it and leave the value as an string as similar
// as posible as seen in Excel
Column<String> stringColumn = (Column<String>) column;
String val = new CellGeneralFormatter().format(cell.getBooleanCellValue());
stringColumn.append(val);
try {
String val = new CellGeneralFormatter().format(cell.getBooleanCellValue());
stringColumn.append(val);
} catch (IllegalFormatException e) {
logger.warn(
"Error formatting cell value {} at ({},{}) to string: {}. Ignoring it's value",
cell.getNumericCellValue(),
cell.getColumnIndex(),
cell.getRowIndex(),
e.getMessage());
stringColumn.append((String) null);
}
}
default:
break;
Expand Down