-
Notifications
You must be signed in to change notification settings - Fork 148
dev/add datatypes to excel columns #230
base: master
Are you sure you want to change the base?
Changes from 9 commits
90303fa
999647c
5a75486
b82e728
8c21387
8919900
aae088f
7ae1b26
77a1b80
84c6f06
b4d9a20
1a34285
3785c91
66749e9
a8ab4b6
57108bc
138b147
5088744
4bfcc41
0701bab
9c3976e
cc85725
23a7781
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,8 @@ | |
| import org.apache.metamodel.util.FileHelper; | ||
| import org.apache.metamodel.util.Resource; | ||
| import org.apache.poi.ss.usermodel.Cell; | ||
| import org.apache.poi.ss.usermodel.CellType; | ||
| import org.apache.poi.ss.usermodel.DateUtil; | ||
| import org.apache.poi.ss.usermodel.Row; | ||
| import org.apache.poi.ss.usermodel.Sheet; | ||
| import org.apache.poi.ss.usermodel.Workbook; | ||
|
|
@@ -63,14 +65,13 @@ public DefaultSpreadsheetReaderDelegate(Resource resource, ExcelConfiguration co | |
| _configuration = configuration; | ||
| } | ||
|
|
||
| @Override | ||
| public Schema createSchema(String schemaName) { | ||
| final MutableSchema schema = new MutableSchema(schemaName); | ||
| final Workbook wb = ExcelUtils.readWorkbook(_resource, true); | ||
| try { | ||
| for (int i = 0; i < wb.getNumberOfSheets(); i++) { | ||
| final Sheet currentSheet = wb.getSheetAt(i); | ||
| final MutableTable table = createTable(wb, currentSheet); | ||
| final MutableTable table = createTable(wb, currentSheet, _configuration.isValidateColumnTypes()); | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| table.setSchema(schema); | ||
| schema.addTable(table); | ||
| } | ||
|
|
@@ -103,7 +104,7 @@ public void notifyTablesModified() { | |
| // do nothing | ||
| } | ||
|
|
||
| private MutableTable createTable(final Workbook wb, final Sheet sheet) { | ||
| private MutableTable createTable(final Workbook wb, final Sheet sheet, boolean validateColumnTypes) { | ||
| final MutableTable table = new MutableTable(sheet.getSheetName(), TableType.TABLE); | ||
|
|
||
SociopathicPixel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (sheet.getPhysicalNumberOfRows() <= 0) { | ||
|
|
@@ -124,11 +125,14 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| while (row == null && rowIterator.hasNext()) { | ||
| row = rowIterator.next(); | ||
| } | ||
|
|
||
| } else { | ||
| row = rowIterator.next(); | ||
| } | ||
|
|
||
| final int columnNameLineNumber = _configuration.getColumnNameLineNumber(); | ||
| final ColumnType[] columnTypes = getColumnTypes(sheet, row); | ||
|
|
||
| if (columnNameLineNumber == ExcelConfiguration.NO_COLUMN_NAME_LINE) { | ||
|
|
||
| // get to the first non-empty line (no matter if lines are skipped | ||
|
|
@@ -148,8 +152,17 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
|
|
||
| for (int j = offset; j < row.getLastCellNum(); j++) { | ||
| final ColumnNamingContext namingContext = new ColumnNamingContextImpl(table, null, j); | ||
| final Column column = new MutableColumn(columnNamingSession.getNextColumnName(namingContext), | ||
| ColumnType.STRING, table, j, true); | ||
| final Column column; | ||
| if (validateColumnTypes) { | ||
|
|
||
| column = | ||
| new MutableColumn(columnNamingSession.getNextColumnName(namingContext), columnTypes[j], | ||
| table, j, true); | ||
| } else { | ||
| column = | ||
| new MutableColumn(columnNamingSession.getNextColumnName(namingContext), | ||
| ColumnType.STRING, table, j, true); | ||
| } | ||
| table.addColumn(column); | ||
| } | ||
| } | ||
|
|
@@ -169,21 +182,84 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| } | ||
|
|
||
| if (hasColumns) { | ||
| createColumns(table, wb, row); | ||
| createColumns(table, wb, row, columnTypes); | ||
| } | ||
| } | ||
|
|
||
| return table; | ||
| } | ||
|
|
||
| private ColumnType[] getColumnTypes(final Sheet sheet, final Row row) { | ||
| final Iterator<Row> data = ExcelUtils.getRowIterator(sheet, _configuration, false); | ||
| final int rowLength = row.getLastCellNum(); | ||
| int eagerness = ExcelConfiguration.EAGER_READ; | ||
| final ColumnType[] columnTypes = new ColumnType[rowLength]; | ||
|
|
||
| while (data.hasNext() && eagerness-- > 0) { | ||
| final Row currentRow = data.next(); | ||
| if (currentRow.getRowNum() < _configuration.getColumnNameLineNumber()) { | ||
| continue; | ||
| } | ||
| for (int index = 0; index < rowLength; index++) { | ||
| if (currentRow.getLastCellNum() == 0) { | ||
| continue; | ||
| } | ||
| columnTypes[index] = getColumnTypeFromRow(columnTypes[index], currentRow, index); | ||
|
||
| } | ||
| } | ||
| return columnTypes; | ||
| } | ||
|
|
||
| private ColumnType getColumnTypeFromRow(final ColumnType columnType, final Row currentRow, int index) { | ||
| if (currentRow.getCell(index) == null) { | ||
| return checkColumnType(ColumnType.STRING, columnType); | ||
| } else { | ||
| CellType cellType = currentRow.getCell(index).getCellType(); | ||
| switch (cellType) { | ||
| case NUMERIC: | ||
| if (DateUtil.isCellDateFormatted(currentRow.getCell(index))) { | ||
| return checkColumnType(ColumnType.DATE, columnType); | ||
| } else { | ||
| return checkColumnType((currentRow.getCell(index).getNumericCellValue() % 1 == 0) | ||
| ? ColumnType.INTEGER : ColumnType.DOUBLE, columnType); | ||
| } | ||
| case BOOLEAN: | ||
| return checkColumnType(ColumnType.BOOLEAN, columnType); | ||
| case ERROR: | ||
| // fall through | ||
| case _NONE: | ||
| // fall through | ||
| case STRING: | ||
| // fall through | ||
| case FORMULA: | ||
| // fall through | ||
| case BLANK: | ||
| // fall through | ||
| default : | ||
| return checkColumnType(ColumnType.STRING, columnType); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private ColumnType checkColumnType(final ColumnType expecetedColumnType, ColumnType columnType) { | ||
| if (columnType != null) { | ||
| if (!columnType.equals(ColumnType.STRING) && !columnType.equals(expecetedColumnType)) { | ||
| return ColumnType.VARCHAR; | ||
| } | ||
| } else { | ||
| return expecetedColumnType; | ||
| } | ||
| return columnType; | ||
| } | ||
|
|
||
| /** | ||
| * Builds columns based on row/cell values. | ||
| * | ||
| * @param table | ||
| * @param wb | ||
| * @param row | ||
| */ | ||
| private void createColumns(MutableTable table, Workbook wb, Row row) { | ||
| private void createColumns(final MutableTable table, final Workbook wb, final Row row, final ColumnType[] columTypes) { | ||
| if (row == null) { | ||
| logger.warn("Cannot create columns based on null row!"); | ||
| return; | ||
|
|
@@ -201,7 +277,12 @@ private void createColumns(MutableTable table, Workbook wb, Row row) { | |
| final ColumnNamingContext columnNamingContext = new ColumnNamingContextImpl(table, intrinsicColumnName, | ||
| j); | ||
| final String columnName = columnNamingSession.getNextColumnName(columnNamingContext); | ||
| final Column column = new MutableColumn(columnName, ColumnType.VARCHAR, table, j, true); | ||
| final Column column; | ||
| if (columTypes == null) { | ||
| column = new MutableColumn(columnName, ColumnType.VARCHAR, table, j, true); | ||
| } else { | ||
| column = new MutableColumn(columnName, columTypes[j], table, j, true); | ||
| } | ||
| table.addColumn(column); | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.