Skip to content

Commit

Permalink
Merge fb71ea8 into eaf1a04
Browse files Browse the repository at this point in the history
  • Loading branch information
prabhatsir authored Dec 22, 2020
2 parents eaf1a04 + fb71ea8 commit f8b84ef
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/main/java/com/poiji/bind/Poiji.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.poiji.bind.mapping.HSSFPropertyStream;
import com.poiji.bind.mapping.PoijiPropertyHelper;
import com.poiji.bind.mapping.UnmarshallerHelper;
import com.poiji.bind.mapping.SheetColumns;
import com.poiji.exception.IllegalCastException;
import com.poiji.exception.InvalidExcelFileExtension;
import com.poiji.exception.PoijiExcelType;
Expand Down Expand Up @@ -359,11 +360,15 @@ public static <T> List<T> fromExcel(final Sheet sheet,
* @see Poiji#fromExcel(Sheet, Class, PoijiOptions)
* @see Poiji#fromExcel(Sheet, Class)
*/
public static <T> void fromExcel(final Sheet sheet,
public static <T> void fromExcel(Sheet sheet,
final Class<T> type,
final PoijiOptions options,
final Consumer<? super T> consumer) {
Objects.requireNonNull(sheet);
if(options.getColumnsToKeep()!=null)
sheet = SheetColumns.sheetAdaptor(sheet,options);
options.setSkip(options.skip()-options.getHeaderStart());
options.setHeaderStart(0);
final Unmarshaller unmarshaller = UnmarshallerHelper.SheetInstance(sheet, options);
unmarshaller.unmarshal(type, consumer);
}
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/poiji/bind/mapping/SheetColumns.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.poiji.bind.mapping;

import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SheetColumns {

public static Sheet sheetAdaptor(Sheet sheet, PoijiOptions options) {

try {
Workbook workbook = sheet.getWorkbook();
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
evaluator.setIgnoreMissingWorkbooks(true);
Workbook result = new XSSFWorkbook();
Sheet resultSheet = result.createSheet();
List<String> headersDone = new ArrayList<>();
List<Integer> dontAvoid = new ArrayList<>();
for (Row row : sheet) {
if (row.getRowNum() < options.getHeaderStart()) continue;
Row newRow = resultSheet.createRow(row.getRowNum() - options.getHeaderStart());
for (Cell cell : row) {
CellType type = null;
if (cell.getCellType() == CellType.FORMULA) type = evaluator.evaluateFormulaCell(cell);
else type = cell.getCellType();
if (row.getRowNum() == options.getHeaderStart()) {
if ((type == CellType.STRING
&& !headersDone.contains(cell.getStringCellValue())
&& options.getColumnsToKeep().contains(cell.getStringCellValue().replace(" ", "").replace("\n", " "))
&& cell.getStringCellValue() != "")) {
dontAvoid.add(cell.getColumnIndex());
headersDone.add(cell.getStringCellValue());
newRow.createCell(cell.getColumnIndex()).setCellValue(cell.getStringCellValue());
continue;
}
} else if (!dontAvoid.contains(cell.getColumnIndex())) continue;
else{
switch (type) {
case BOOLEAN:
newRow.createCell(cell.getColumnIndex()).setCellValue(cell.getBooleanCellValue());
break;
case NUMERIC:
newRow.createCell(cell.getColumnIndex()).setCellValue(cell.getNumericCellValue());
break;
case STRING:
newRow
.createCell(cell.getColumnIndex())
.setCellValue(cell.getStringCellValue().replace(" ", "").replace("\n", " "));
break;
case BLANK:
newRow.createCell(cell.getColumnIndex()).setCellValue("");
}
}

}
}
return resultSheet;
} catch (Exception exception) {
throw new PoijiException("Problem occurred while reading data", exception);
}
}

}
31 changes: 27 additions & 4 deletions src/main/java/com/poiji/option/PoijiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.poiji.exception.PoijiException;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

import static com.poiji.util.PoijiConstants.DEFAULT_DATE_FORMATTER;
Expand Down Expand Up @@ -46,6 +47,16 @@ public final class PoijiOptions {
private boolean disableXLSXNumberCellFormat;
private String listDelimiter;
private Formatting formatting;
private List columnsToKeep;

public List getColumnsToKeep() {
return columnsToKeep;
}

private PoijiOptions setColumnsToKeep(List columnsToKeep) {
this.columnsToKeep = columnsToKeep;
return this;
}

public PoijiNumberFormat getPoijiNumberFormat() {
return numberFormat;
Expand All @@ -69,7 +80,7 @@ private PoijiOptions() {
super();
}

private PoijiOptions setSkip(int skip) {
public PoijiOptions setSkip(int skip) {
this.skip = skip;
return this;
}
Expand Down Expand Up @@ -208,7 +219,7 @@ public int getHeaderCount() {
return headerCount;
}

private PoijiOptions setHeaderStart(int headerStart) {
public PoijiOptions setHeaderStart(int headerStart) {
this.headerStart = headerStart;
return this;
}
Expand Down Expand Up @@ -308,6 +319,7 @@ public static class PoijiOptionsBuilder {
private boolean namedHeaderMandatory;
private boolean disabledXLSXNumberCellFormat;
private String listDelimiter = ",";
private List columnsToKeep;

private PoijiOptionsBuilder() {
}
Expand Down Expand Up @@ -407,7 +419,8 @@ public PoijiOptions build() {
.setNamedHeaderMandatory(namedHeaderMandatory)
.disableXLSXNumberCellFormat(disabledXLSXNumberCellFormat)
.setListDelimiter(listDelimiter)
.setFormatting(formatting);
.setFormatting(formatting)
.setColumnsToKeep(columnsToKeep);
}

/**
Expand Down Expand Up @@ -626,8 +639,18 @@ public PoijiOptionsBuilder poijiNumberFormat(final PoijiNumberFormat numberForma

/**
* Set true if all headers named in {@link ExcelCellName} are mandatory, otherwise false
* This feature needs headerstart and skip as mandatory parameters
* @param columnsToKeep the headers that should be included
*/
public PoijiOptionsBuilder columnsToKeep(List columnsToKeep) {
this.columnsToKeep = columnsToKeep;
return this;
}

/**
* Provide columns to keep in case of repeated headers
*
* @param namedHeaderMandatory fieldas are mandatory or not
* @param namedHeaderMandatory
*/
public PoijiOptionsBuilder namedHeaderMandatory(boolean namedHeaderMandatory) {
this.namedHeaderMandatory = namedHeaderMandatory;
Expand Down

0 comments on commit f8b84ef

Please sign in to comment.