Skip to content

Commit

Permalink
Merge 1523508 into 12283d9
Browse files Browse the repository at this point in the history
  • Loading branch information
bsungur committed Feb 9, 2020
2 parents 12283d9 + 1523508 commit d4a47f6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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.util.IOUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
Expand Down Expand Up @@ -59,25 +60,29 @@ abstract class HSSFUnmarshaller implements Unmarshaller {
@Override
public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
Workbook workbook = workbook();
Optional<String> maybeSheetName = SheetNameExtractor.getSheetName(type, options);
try {
Optional<String> maybeSheetName = SheetNameExtractor.getSheetName(type, options);

Sheet sheet = this.getSheetToProcess(workbook, options, maybeSheetName.orElse(null));
Sheet sheet = this.getSheetToProcess(workbook, options, maybeSheetName.orElse(null));

int skip = options.skip();
int maxPhysicalNumberOfRows = sheet.getPhysicalNumberOfRows() + 1 - skip;
int skip = options.skip();
int maxPhysicalNumberOfRows = sheet.getPhysicalNumberOfRows() + 1 - skip;

loadColumnTitles(sheet, maxPhysicalNumberOfRows);
loadColumnTitles(sheet, maxPhysicalNumberOfRows);

for (Row currentRow : sheet) {
if (!skip(currentRow, skip) && !isRowEmpty(currentRow)) {
internalCount += 1;
for (Row currentRow : sheet) {
if (!skip(currentRow, skip) && !isRowEmpty(currentRow)) {
internalCount += 1;

if (limit != 0 && internalCount > limit)
return;
if (limit != 0 && internalCount > limit)
return;

T instance = deserializeRowToInstance(currentRow, type);
consumer.accept(instance);
T instance = deserializeRowToInstance(currentRow, type);
consumer.accept(instance);
}
}
} finally {
IOUtils.closeQuietly(workbook);
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/poiji/deserialize/FileClosedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.poiji.deserialize;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.ProductExcelDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

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

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

@RunWith(Parameterized.class)
public class FileClosedTest {

private String path;

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

@Parameters
public static Object[] excel() {
return new Object[]{"src/test/resources/test.xls", "src/test/resources/test.xlsx"};
}

@Test
public void shouldMapExcelToJava() {
File file = new File(path);
System.out.println(file.toString());
List<ProductExcelDTO> products = Poiji.fromExcel(file, ProductExcelDTO.class);
assertThat(products, notNullValue());
assertThat(products.size(), not(0));
assertThat(file.renameTo(file), is(true));
}

}

0 comments on commit d4a47f6

Please sign in to comment.