-
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 3 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; | ||
|
|
@@ -128,6 +130,9 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| row = rowIterator.next(); | ||
| } | ||
|
|
||
| // Get first 1000 rows for the eager-read | ||
| final ColumnType[] columnTypes = getColumnTypes(sheet, row); | ||
|
|
||
| final int columnNameLineNumber = _configuration.getColumnNameLineNumber(); | ||
| if (columnNameLineNumber == ExcelConfiguration.NO_COLUMN_NAME_LINE) { | ||
|
|
||
|
|
@@ -137,27 +142,22 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| while (row == null && rowIterator.hasNext()) { | ||
| row = rowIterator.next(); | ||
| } | ||
|
|
||
| // build columns without any intrinsic column names | ||
| final ColumnNamingStrategy columnNamingStrategy = _configuration.getColumnNamingStrategy(); | ||
| try (final ColumnNamingSession columnNamingSession = columnNamingStrategy.startColumnNamingSession()) { | ||
| final int offset = getColumnOffset(row); | ||
| for (int i = 0; i < offset; i++) { | ||
| columnNamingSession.getNextColumnName(new ColumnNamingContextImpl(i)); | ||
| } | ||
|
|
||
| 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); | ||
| columnTypes[j], table, j, true); | ||
| table.addColumn(column); | ||
| } | ||
| } | ||
|
|
||
| } else { | ||
|
|
||
| boolean hasColumns = true; | ||
|
|
||
| // iterate to the column name line number (if above 1) | ||
| for (int j = 1; j < columnNameLineNumber; j++) { | ||
| if (rowIterator.hasNext()) { | ||
|
|
@@ -167,50 +167,109 @@ private MutableTable createTable(final Workbook wb, final Sheet sheet) { | |
| break; | ||
| } | ||
| } | ||
|
|
||
| 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 = 1000; | ||
| final ColumnType[] columnTypes = new ColumnType[rowLength]; | ||
|
|
||
| while (data.hasNext() && eagerness-- > 0) { | ||
| Row currentRow = data.next(); | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| for (int index = 0; index < rowLength; index++) { | ||
| if (currentRow.getLastCellNum() == 0) { | ||
| continue; | ||
| } | ||
| if (currentRow.getCell(index) == null) { | ||
|
||
| checkColumnType(ColumnType.STRING, columnTypes, index); | ||
| } else { | ||
| CellType cellType = currentRow.getCell(index).getCellType(); | ||
| switch (cellType) { | ||
| case NUMERIC: | ||
| if (DateUtil.isCellDateFormatted(currentRow.getCell(index))) { | ||
| checkColumnType(ColumnType.DATE, columnTypes, index); | ||
| } else { | ||
| checkColumnType((currentRow.getCell(index).getNumericCellValue() % 1 == 0) | ||
| ? ColumnType.INTEGER : ColumnType.DOUBLE, columnTypes, index); | ||
| } | ||
| break; | ||
| case BOOLEAN: | ||
| checkColumnType(ColumnType.BOOLEAN, columnTypes, index); | ||
| break; | ||
| case ERROR: | ||
| // fall through | ||
| break; | ||
| case _NONE: | ||
| // fall through | ||
| case STRING: | ||
| // fall through | ||
| case FORMULA: | ||
| // fall through | ||
| case BLANK: | ||
| checkColumnType(ColumnType.STRING, columnTypes, index); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return columnTypes; | ||
| } | ||
|
|
||
| private void checkColumnType(final ColumnType columnType, final ColumnType[] columnTypes, int index) { | ||
| if (columnTypes[index] != null) { | ||
| if (!columnTypes[index].equals(ColumnType.STRING) && !columnTypes[index].equals(columnType)) { | ||
| columnTypes[index] = ColumnType.STRING; | ||
| } | ||
| } else { | ||
| columnTypes[index] = 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(MutableTable table, Workbook wb, Row row, ColumnType[] columTypes) { | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (row == null) { | ||
| logger.warn("Cannot create columns based on null row!"); | ||
| return; | ||
| } | ||
| final short rowLength = row.getLastCellNum(); | ||
|
|
||
| final int offset = getColumnOffset(row); | ||
|
|
||
| // build columns based on cell values. | ||
| try (final ColumnNamingSession columnNamingSession = _configuration.getColumnNamingStrategy() | ||
| try (final ColumnNamingSession columnNamingSession = _configuration | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .getColumnNamingStrategy() | ||
| .startColumnNamingSession()) { | ||
| for (int j = offset; j < rowLength; j++) { | ||
| final Cell cell = row.getCell(j); | ||
| final String intrinsicColumnName = ExcelUtils.getCellValue(wb, cell); | ||
| 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); | ||
| Column column = null; | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Gets the column offset (first column to include). This is dependent on | ||
| * the row used for column processing and whether the skip empty columns | ||
| * property is set. | ||
| * Gets the column offset (first column to include). This is dependent on the | ||
SociopathicPixel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * row used for column processing and whether the skip empty columns property is | ||
| * set. | ||
| * | ||
| * @param row | ||
| * @return | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.