-
Notifications
You must be signed in to change notification settings - Fork 150
dev/add datatypes to excel columns #230
base: master
Are you sure you want to change the base?
Changes from all 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 |
---|---|---|
|
@@ -20,21 +20,23 @@ | |
|
||
import java.util.Date; | ||
|
||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.CellStyle; | ||
import org.apache.poi.ss.usermodel.FillPatternType; | ||
import org.apache.poi.ss.usermodel.Font; | ||
import org.apache.poi.ss.usermodel.HorizontalAlignment; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.metamodel.MetaModelException; | ||
import org.apache.metamodel.data.Style; | ||
import org.apache.metamodel.data.Style.Color; | ||
import org.apache.metamodel.data.Style.SizeUnit; | ||
import org.apache.metamodel.data.Style.TextAlignment; | ||
import org.apache.metamodel.insert.AbstractRowInsertionBuilder; | ||
import org.apache.metamodel.insert.RowInsertionBuilder; | ||
import org.apache.metamodel.schema.Column; | ||
import org.apache.metamodel.schema.ColumnType; | ||
import org.apache.metamodel.schema.Table; | ||
import org.apache.metamodel.util.LazyRef; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.CellStyle; | ||
import org.apache.poi.ss.usermodel.FillPatternType; | ||
import org.apache.poi.ss.usermodel.Font; | ||
import org.apache.poi.ss.usermodel.HorizontalAlignment; | ||
import org.apache.poi.ss.usermodel.Row; | ||
|
||
/** | ||
* {@link RowInsertionBuilder} for excel spreadsheets. | ||
|
@@ -149,8 +151,33 @@ protected CellStyle fetch() { | |
cell.setCellStyle(cellStyle.get()); | ||
} | ||
} | ||
validateUpdateType(row); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what the value is of adding this here. I think that for now it may be best to skip the "validation" part. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see: ExcelDataContextTest.testUpdateDifferentDataTypes It looks if the Column has a specific ColumnType and if so it will validate the given row. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SociopathicPixel I'm sorry, but I still don't get why this has been added here. |
||
} | ||
} | ||
|
||
private void validateUpdateType(final Row original) { | ||
for (int index = 0; index < this.getColumns().length; index++) { | ||
final ColumnType columnType = getColumns()[index].getType(); | ||
if (columnType != null && getValues()[index] != null) { | ||
switch (columnType.getName()) { | ||
case "INTEGER": | ||
try { | ||
Integer.decode(getValues()[index].toString()); | ||
} catch (NumberFormatException ex) { | ||
throw new MetaModelException(original.getCell(index) | ||
+ " should be an Integer!"); | ||
} | ||
break; | ||
case "STRING": | ||
// fall through | ||
case "VARCHAR": | ||
// fall through | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Converts a percentage based font size to excel "pt" scale. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -171,22 +171,22 @@ public static void writeAndCloseWorkbook(ExcelDataContext dataContext, final Wor | |
|
||
} | ||
|
||
public static String getCellValue(Workbook wb, Cell cell) { | ||
public static Object getCellValue(Workbook wb, Cell cell) { | ||
if (cell == null) { | ||
return null; | ||
} | ||
|
||
final String cellCoordinate = "(" + cell.getRowIndex() + "," + cell.getColumnIndex() + ")"; | ||
|
||
final String result; | ||
final Object result; | ||
|
||
switch (cell.getCellType()) { | ||
case BLANK: | ||
case _NONE: | ||
result = null; | ||
break; | ||
case BOOLEAN: | ||
result = Boolean.toString(cell.getBooleanCellValue()); | ||
result = cell.getBooleanCellValue(); | ||
break; | ||
case ERROR: | ||
String errorResult; | ||
|
@@ -237,7 +237,7 @@ public static String getCellValue(Workbook wb, Cell cell) { | |
return result; | ||
} | ||
|
||
private static String getFormulaCellValue(Workbook wb, Cell cell) { | ||
private static Object getFormulaCellValue(Workbook wb, Cell cell) { | ||
// first try with a cached/precalculated value | ||
try { | ||
double numericCellValue = cell.getNumericCellValue(); | ||
|
@@ -414,13 +414,13 @@ public static Iterator<Row> getRowIterator(Sheet sheet, ExcelConfiguration confi | |
*/ | ||
public static DefaultRow createRow(Workbook workbook, Row row, DataSetHeader header) { | ||
final int size = header.size(); | ||
final String[] values = new String[size]; | ||
final Object[] values = new Object[size]; | ||
final Style[] styles = new Style[size]; | ||
if (row != null) { | ||
for (int i = 0; i < size; i++) { | ||
final int columnNumber = header.getSelectItem(i).getColumn().getColumnNumber(); | ||
final Cell cell = row.getCell(columnNumber); | ||
final String value = ExcelUtils.getCellValue(workbook, cell); | ||
final Object value = ExcelUtils.getCellValue(workbook, cell); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning an Object doesn't do the trick, we know based on the column type, what type of object should be returned, so use that to either return a String, Integer, Boolean or Date object. |
||
final Style style = ExcelUtils.getCellStyle(workbook, cell); | ||
values[i] = value; | ||
styles[i] = style; | ||
|
Uh oh!
There was an error while loading. Please reload this page.