Skip to content

Commit

Permalink
Merge 45f9364 into 32d1215
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed May 30, 2021
2 parents 32d1215 + 45f9364 commit 06d1e19
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 33 deletions.
58 changes: 30 additions & 28 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 Expand Up @@ -59,31 +59,33 @@ com.poiji.bind.Poiji#fromExcelProperties(java.io.InputStream, com.poiji.exceptio

.`PoijiOptions.PoijiOptionsBuilder` Structure
----
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#settings()
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#build()
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateLenient(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateRegex(String)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#datePattern(String)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#dateTimeFormatter(java.time.format.DateTimeFormatter)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#ignoreHiddenSheets(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#password(String)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#preferNullOverDefault(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#settings(int)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#sheetIndex(int)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#skip(int)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#limit(int)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#trimCellValue(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#headerStart(int)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#withCasting(Casting)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#withFormatting(Formatting)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#caseInsensitive(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#ignoreWhitespaces(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#namedHeaderMandatory(boolean)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#poijiNumberFormat(PoijiNumberFormat)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#poijiLogCellFormat(PoijiLogCellFormat)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#disableXLSXNumberCellFormat()
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#addListDelimiter(String)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder#setLocale(java.util.Locale)
com.poiji.option.PoijiOptions.PoijiOptionsBuilder
#settings()
#build()
#dateLenient(boolean)
#dateRegex(String)
#datePattern(String)
#dateTimeFormatter(java.time.format.DateTimeFormatter)
#ignoreHiddenSheets(boolean)
#password(String)
#preferNullOverDefault(boolean)
#settings(int)
#sheetIndex(int)
#skip(int)
#limit(int)
#trimCellValue(boolean)
#headerStart(int)
#withCasting(Casting)
#withFormatting(Formatting)
#caseInsensitive(boolean)
#ignoreWhitespaces(boolean)
#namedHeaderMandatory(boolean)
#poijiNumberFormat(PoijiNumberFormat)
#poijiLogCellFormat(PoijiLogCellFormat)
#disableXLSXNumberCellFormat()
#addListDelimiter(String)
#setLocale(java.util.Locale)
#rawData(boolean)
----

=== Feature 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.isRawData() && 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
28 changes: 28 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,28 @@
package com.poiji.bind.mapping;

import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.DateUtil;
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) {
if (!DateUtil.isADateFormat(formatIndex, formatString) && options.isRawData()) {
return NumberToTextConverter.toText(value);
} else {
return super.formatRawCellContents(value, formatIndex, formatString, use1904Windowing);
}
}
}
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
27 changes: 26 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 rawData;

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

public boolean isRawData() {
return rawData;
}

private PoijiOptions setRawData(boolean rawData) {
this.rawData = rawData;
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 rawData;

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

}

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

/**
* Use this option to get the underlying/original/non-visible/raw cell value. The cell must be a numeric type.
*
* @param status set true to retrieve the underlying data in the excel file.
* @return this
*/
public PoijiOptionsBuilder rawData(boolean status) {
this.rawData = status;
return this;
}
}

}
47 changes: 47 additions & 0 deletions src/test/java/com/poiji/deserialize/RawValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.poiji.deserialize;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.RowModel;
import com.poiji.option.PoijiOptions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
* Created by hakan on 02/08/2018
*/
@RunWith(Parameterized.class)
public class RawValueTest {

private String path;

public RawValueTest(String path) {
this.path = path;
}

@Parameterized.Parameters(name = "{index}: ({0})={1}")
public static Iterable<Object[]> queries() {
return Arrays.asList(new Object[][]{
{"src/test/resources/raw_value.xls"},
{"src/test/resources/raw_value.xlsx"},
});
}

@Test
public void shouldMapCalculations() {

PoijiOptions options = PoijiOptions.PoijiOptionsBuilder.settings().headerCount(0).rawData(true).build();
List<RowModel> models = Poiji.fromExcel(new File(path), RowModel.class, options);

for (RowModel model : models) {
assertThat(model.getRowValue(), is(57363987456321L));
}
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/poiji/deserialize/model/RowModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.poiji.deserialize.model;

import com.poiji.annotation.ExcelCell;

/**
* Created by hakan on 16.05.2021
*/
public class RowModel {

@ExcelCell(0)
private Long rowValue;

public Long getRowValue() {
return rowValue;
}

public void setRowValue(Long rowValue) {
this.rowValue = rowValue;
}
}
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 06d1e19

Please sign in to comment.