dan-s1 commented on code in PR #9290:
URL: https://github.com/apache/nifi/pull/9290#discussion_r1796926374
##########
nifi-extension-bundles/nifi-poi-bundle/nifi-poi-services/src/main/java/org/apache/nifi/excel/ExcelUtils.java:
##########
@@ -16,15 +16,45 @@
*/
package org.apache.nifi.excel;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.DataFormatter;
+import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
+import java.util.Locale;
+
public class ExcelUtils {
static final String FIELD_NAME_PREFIX = "column_";
+ private static final DataFormatter DATA_FORMATTER = new
DataFormatter(Locale.getDefault());
+
+ static {
+ DATA_FORMATTER.setUseCachedValuesForFormulaCells(true);
+ }
private ExcelUtils() {
}
public static boolean hasCells(final Row row) {
return row != null && row.getFirstCellNum() != -1;
}
+
+ public static String getFormattedCellValue(Cell cell) {
+ if (cell != null) {
+ /* DataFormatter should be used in most cases unless an exception
will be thrown or
+ string localization may occur which could result in later
exceptions. A cell which has a formula cannot
+ format a cell which has an error value and an exception will be
thrown. In addition, a cell whose type
+ is numeric when formatted will take the current locale into
consideration which could prevent later parsing
+ as a float/double.*/
+ if (cell.getCellType().equals(CellType.FORMULA) &&
cell.getCachedFormulaResultType().equals(CellType.ERROR)) {
+ return cell.getStringCellValue();
+ } else if (cell.getCellType().equals(CellType.NUMERIC) &&
!DateUtil.isCellDateFormatted(cell)) {
+ final double numericCellValue = cell.getNumericCellValue();
+ return numericCellValue % 1 == 0 ? Long.toString((long)
numericCellValue) : Double.toString(numericCellValue);
Review Comment:
Would this be better `numericCellValue == (long) numericCellValue` ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]