Skip to content

Commit

Permalink
enable to get the original value of a number
Browse files Browse the repository at this point in the history
Signed-off-by: Hakan Ozler <ozler.hakan@gmail.com>
  • Loading branch information
ozlerhakan committed May 16, 2021
1 parent 32d1215 commit 013d6d9
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.NumberToTextConverter;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -239,7 +240,12 @@ private <T> void constructTypeValue(Row currentRow, T instance, Field field, Fie
if (annotationDetail.isDisabledCellFormat()) {
cell.setCellStyle(null);
}
String value = dataFormatter.formatCellValue(cell, baseFormulaEvaluator);
String value;
if (options.isReturnRawValues() && cell.getCellType() == CellType.NUMERIC) {
value = NumberToTextConverter.toText(cell.getNumericCellValue());
} else {
value = dataFormatter.formatCellValue(cell, baseFormulaEvaluator);
}
Object data = casting.castValue(field, value, currentRow.getRowNum(), annotationDetail.getColumn(), options);
setFieldData(instance, field, data);
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/poiji/bind/mapping/PoijiDataFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.poiji.bind.mapping;

import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.util.NumberToTextConverter;

/**
* Created by hakan on 16.05.2021
*/
public class PoijiDataFormatter extends DataFormatter {

PoijiOptions options;

public PoijiDataFormatter(PoijiOptions options) {
super();
this.options = options;
}

@Override
public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) {
String cellContents = super.formatRawCellContents(value, formatIndex, formatString, use1904Windowing);

if (options.isReturnRawValues()) {
return NumberToTextConverter.toText(value);
} else {
return cellContents;
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.XMLHelper;
import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable;
Expand Down Expand Up @@ -102,7 +101,7 @@ private <T> void processSheet(StylesTable styles,
InputStream sheetInputStream,
Consumer<? super T> consumer) {

DataFormatter formatter = new DataFormatter();
PoijiDataFormatter formatter = new PoijiDataFormatter(options);
InputSource sheetSource = new InputSource(sheetInputStream);
try {
PoijiHandler<T> poijiHandler = new PoijiHandler<>(type, options, consumer);
Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/poiji/option/PoijiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class PoijiOptions {
private String listDelimiter;
private Formatting formatting;
private Locale locale;
private boolean returnRawValues;

public PoijiNumberFormat getPoijiNumberFormat() {
return numberFormat;
Expand Down Expand Up @@ -294,6 +295,15 @@ private PoijiOptions setLocale(Locale locale) {
return this;
}

public boolean isReturnRawValues() {
return returnRawValues;
}

private PoijiOptions setReturnRawValues(boolean returnRawValues) {
this.returnRawValues = returnRawValues;
return this;
}

public static class PoijiOptionsBuilder {

private int sheetIndex;
Expand Down Expand Up @@ -322,6 +332,7 @@ public static class PoijiOptionsBuilder {
private boolean disabledXLSXNumberCellFormat;
private String listDelimiter = ",";
private Locale locale = Locale.US;
private boolean returnRawValues;

private PoijiOptionsBuilder() {
}
Expand Down Expand Up @@ -434,7 +445,9 @@ public PoijiOptions build() {
.disableXLSXNumberCellFormat(disabledXLSXNumberCellFormat)
.setListDelimiter(listDelimiter)
.setFormatting(formatting)
.setLocale(locale);
.setLocale(locale)
.setReturnRawValues(returnRawValues);

}

/**
Expand Down Expand Up @@ -695,5 +708,16 @@ public PoijiOptionsBuilder withFormatting(Formatting formatting) {
this.formatting = formatting;
return this;
}

/**
* Use this option to get the underlying/original/non-visible cell value. The cell must be a numeric type.
*
* @return this
*/
public PoijiOptionsBuilder returnNumericRawValues() {
this.returnRawValues = true;
return this;
}
}

}
Binary file added src/test/resources/raw_value.xls
Binary file not shown.
Binary file added src/test/resources/raw_value.xlsx
Binary file not shown.

0 comments on commit 013d6d9

Please sign in to comment.