Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.5.0 added readSheetNames() and fixed @ #5

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ In your Maven/Gradle project, first add the corresponding dependency:
<dependency>
<groupId>io.github.vaa25</groupId>
<artifactId>poiji2</artifactId>
<version>1.4.1</version>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
Expand All @@ -29,7 +29,7 @@ In your Maven/Gradle project, first add the corresponding dependency:
[source,groovy]
----
dependencies {
implementation 'io.github.vaa25:poiji2:1.4.1'
implementation 'io.github.vaa25:poiji2:1.5.0'
implementation 'org.apache.poi:poi-ooxml:5.2.5'
}
----
Expand All @@ -52,3 +52,4 @@ Also:
- Poiji2 can read and write huge xlsx files (see HugeTest.java)
- Poiji2 (since v1.4.0) can work with immutable java classes (see IgnoreTest.java). lombok @Value and java records applicable also.
- Poiji2 (since v1.4.1) can work with immutable java classes with many constructors (see ExcelConstructorTest.java). Apply @ExcelConstructor to choose one of.
- Poiji2 (since v1.5.0) can read sheet names (see ReadSheetNamesTest.java).
29 changes: 24 additions & 5 deletions src/main/java/com/poiji/bind/FromExcel.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.poiji.exception.PoijiExcelType;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ss.usermodel.Sheet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand All @@ -13,7 +15,6 @@
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.Sheet;

public class FromExcel<T> {

Expand Down Expand Up @@ -42,6 +43,12 @@ public List<T> toList() {
return result;
}

public List<String> readSheetNames() {
validateSource();
validateOptions();
return source.getDeserializer(options).readSheetNames();
}

public Stream<T> toStream() {
validate();
final Stream<T> stream = source.getDeserializer(options).stream(javaType);
Expand All @@ -53,17 +60,29 @@ public Stream<T> toStream() {
}

private void validate() {
if (source == null) {
throw new PoijiException("Source must be set");
}
validateSource();
validateJavaType();
validateOptions();
}

private void validateJavaType() {
if (javaType == null) {
throw new PoijiException("Class must be set");
}
}

private void validateOptions() {
if (options == null){
options = PoijiOptions.PoijiOptionsBuilder.settings().build();
}
}

private void validateSource() {
if (source == null) {
throw new PoijiException("Source must be set");
}
}

public FromExcel<T> withConsumer(final Consumer<? super T> consumer) {
this.consumer = consumer;
return this;
Expand Down Expand Up @@ -173,4 +192,4 @@ public Unmarshaller getDeserializer(final PoijiOptions options) {
}

}
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/poiji/bind/Unmarshaller.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.poiji.bind;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

Expand All @@ -11,4 +12,6 @@ public interface Unmarshaller {
<T> void unmarshal(Class<T> type, Consumer<? super T> consumer);

<T> Stream<T> stream(Class<T> type);

List<String> readSheetNames();
}
26 changes: 21 additions & 5 deletions src/main/java/com/poiji/bind/mapping/CsvUnmarshallerStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.poiji.bind.Unmarshaller;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.poiji.util.ReflectUtil;

import java.io.*;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -47,6 +49,20 @@ public <T> Stream<T> stream(final Class<T> type) {
}
}

/**
* Excel uses file name as sheet name for CSV format.
*/
@Override
public List<String> readSheetNames() {
final InputStream stream = poijiStream.stream();
if (stream instanceof FileInputStream) {
final String path = ReflectUtil.getFieldData("path", stream);
final String fileName = Paths.get(path).getFileName().toString();
return Collections.singletonList(fileName.substring(0, fileName.lastIndexOf(".")));
}
return options.preferNullOverDefault() ? null : Collections.emptyList();
}

private static class BomInputStream extends InputStream{

private final InputStream inner;
Expand All @@ -73,7 +89,7 @@ public Optional<String> getCharset(){
}

@Override
public int read() throws IOException {
public int read() {
return 0;
}

Expand Down
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 @@ -4,22 +4,16 @@
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.save.TransposeUtil;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.BaseFormulaEvaluator;
import org.apache.poi.ss.usermodel.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.BaseFormulaEvaluator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
* This is the main class that converts the excel sheet fromExcel Java object
Expand Down Expand Up @@ -47,6 +41,17 @@ public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
}
}

@Override
public List<String> readSheetNames() {
try (final HSSFWorkbook workbook = (HSSFWorkbook) workbook()) {
final List<String> result = new ArrayList<>();
workbook.forEach(sheet-> result.add(sheet.getSheetName()));
return result;
} catch (final IOException e) {
throw new PoijiException("Problem occurred while closing HSSFWorkbook", e);
}
}

private <T> Sheet getSheet(final Class<T> type, final HSSFWorkbook workbook) {
if (options.getTransposed()){
TransposeUtil.transpose(workbook);
Expand Down
21 changes: 5 additions & 16 deletions src/main/java/com/poiji/bind/mapping/ReadMappedFields.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
package com.poiji.bind.mapping;

import com.poiji.annotation.ExcelCell;
import com.poiji.annotation.ExcelCellName;
import com.poiji.annotation.ExcelCellRange;
import com.poiji.annotation.ExcelList;
import com.poiji.annotation.ExcelParseExceptions;
import com.poiji.annotation.ExcelRow;
import com.poiji.annotation.ExcelUnknownCells;
import com.poiji.annotation.ExcelWriteOnly;
import com.poiji.annotation.*;
import com.poiji.config.Casting;
import com.poiji.exception.ExcelParseException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.AnnotationUtil;
import com.poiji.util.ReflectUtil;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import static com.poiji.annotation.ExcelCellName.ABSENT_ORDER;
import static java.util.Arrays.asList;
Expand Down Expand Up @@ -127,7 +116,7 @@ public void setCellInData(final int row, final int column, final String content,
for (final Field unknownField : unknownFields) {
final Object unknownData = data.get(unknownField);
if (unknownData == null) {
final Map<String, String> map = new HashMap<>();
final Map<String, String> map = ReflectUtil.newMap(unknownField);
data.put(unknownField, map);
map.put(unknownColumns.get(column), content);
} else {
Expand Down Expand Up @@ -235,7 +224,7 @@ private List<String> getPossibleFieldNames(final ExcelCellName annotation) {
private List<Field> parseUnknownCells(final List<Field> fields) {
final List<Field> rest = new ArrayList<>(fields.size());
for (final Field field : fields) {
if (field.getAnnotation(ExcelUnknownCells.class) != null && field.getType().isAssignableFrom(Map.class)) {
if (field.isAnnotationPresent(ExcelUnknownCells.class) && Map.class.isAssignableFrom(field.getType())) {
unknownFields.add(field);
ReflectUtil.setAccessible(field);
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/poiji/bind/mapping/SheetUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
* Created by hakan on 11.10.2020
*/
Expand All @@ -29,6 +32,11 @@ public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {
processRowsToObjects(sheet, type, consumer);
}

@Override
public List<String> readSheetNames() {
return Collections.singletonList(sheet.getSheetName());
}

private void setBaseFormulaEvaluator() {
Workbook workbook = workbook();
if (workbook instanceof HSSFWorkbook) {
Expand Down
Loading
Loading