Skip to content

Commit

Permalink
Merge ed94932 into 32d1215
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed May 16, 2021
2 parents 32d1215 + ed94932 commit 191dbbc
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
:toclevels: 1

= Poiji
:version: v3.1.0
:version: v3.1.1


image:https://travis-ci.org/ozlerhakan/poiji.svg?branch=master["Build Status", link="https://travis-ci.org/ozlerhakan/poiji"] image:https://api.codacy.com/project/badge/Grade/6587e90886184da29a1b7c5634695c9d["Codacy code quality", link="https://www.codacy.com/app/ozlerhakan/poiji?utm_source=github.com&utm_medium=referral&utm_content=ozlerhakan/poiji&utm_campaign=Badge_Grade"] image:https://coveralls.io/repos/github/ozlerhakan/poiji/badge.svg?branch=master["Coverage Status", link="https://coveralls.io/github/ozlerhakan/poiji?branch=master"] image:https://img.shields.io/badge/apache.poi-5.0.0-brightgreen.svg[] image:https://app.fossa.com/api/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji.svg?type=shield["FOSSA Status", link="https://app.fossa.com/projects/git%2Bgithub.com%2Fozlerhakan%2Fpoiji?ref=badge_shield"] image:https://img.shields.io/badge/license-MIT-blue.svg[]
Expand All @@ -23,15 +23,15 @@ In your Maven/Gradle project, first add the corresponding dependency:
<dependency>
<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
</dependency>
----

.gradle
[source,groovy]
----
dependencies {
compile 'com.github.ozlerhakan:poiji:3.1.0'
compile 'com.github.ozlerhakan:poiji:3.1.1'
}
----

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
<packaging>jar</packaging>

<name>poiji</name>
Expand Down
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 191dbbc

Please sign in to comment.