diff --git a/poi-util/src/main/java/de/muehlencord/shared/poi/PoiUtil.java b/poi-util/src/main/java/de/muehlencord/shared/poi/PoiUtil.java index de0e26b..0c3468e 100644 --- a/poi-util/src/main/java/de/muehlencord/shared/poi/PoiUtil.java +++ b/poi-util/src/main/java/de/muehlencord/shared/poi/PoiUtil.java @@ -144,6 +144,23 @@ public abstract class PoiUtil { currentCell.setCellValue(date); } + /** + * Returns the string value from the given cell. If the value is mandatory but the cell does not contain a value a {@link PoiException} is thrown. + * + * @param row the row to read the value from + * @param cellNumber the cellnumber (starting with 0) of the {@link Row} to read the content from. + * @param mandatory if set to true, a {@link PoiException} is thrown, if the specified cell, does not contain any value. Otherwise null is returned if the cell is empty. + * @return the String value of the specified cell. + * @throws PoiException if the cell value cannot be read or if the cell is empty and mandatory has been set to true. + */ + public static String getStringValue(Row row, int cellNumber, boolean mandatory) throws PoiException { + String returnValue = getStringValue(row, cellNumber, false, null); + if (returnValue == null) { + throw new PoiException("Value in row=" + row.getRowNum() + ", cell=" + cellNumber + " is mandatory but not available"); + } + return returnValue; + } + public static String getStringValue(Row row, int cellNumber) throws PoiException { return getStringValue(row, cellNumber, false, null); } @@ -239,8 +256,7 @@ public abstract class PoiUtil { } /** - * remove charecters which often cause troubles in other applications - e.g. - * em dash (long dash) is created by Excel automatically + * remove charecters which often cause troubles in other applications - e.g. em dash (long dash) is created by Excel automatically * * @param sourceValue the input string to execute the replacement * @return sourceValue with replaced characters