Skip to content

Commit

Permalink
feat: add test file and fix numeric control
Browse files Browse the repository at this point in the history
  • Loading branch information
kadioglumf committed Nov 13, 2023
1 parent 6c0096a commit ff2fabe
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/kadioglumf/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public static void main(String[] args) throws Exception {
LocaleContextHolder.setLocale(new Locale("tr", "TR"));

File file = new File(System.getProperty("user.dir") + "/src/main/resources/test.xlsx");
ReaderService<TestExcelDto> readerService = new ReaderService<>(TestExcelDto.class);
ReaderService readerService = new ReaderService();

List<TestExcelDto> list =readerService.readFile(new FileInputStream(file), FilenameUtils.getExtension(file.getName()), true);
List<TestExcelDto> list = readerService.readFile(TestExcelDto.class, new FileInputStream(file), FilenameUtils.getExtension(file.getName()), true);
list.forEach(System.out::println);
}
}
31 changes: 13 additions & 18 deletions src/main/java/com/kadioglumf/dto/excel/TestExcelDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.Date;

@Data
Expand All @@ -15,39 +19,30 @@ public class TestExcelDto implements Serializable {
private Date date;

@ExcelColumn(columnIndex = 1)
private LocalTime time;
private LocalTime localTime;

@ExcelColumn(columnIndex = 2)
private BigDecimal total;

@ExcelColumn(columnIndex = 3)
private BigDecimal dogalgaz;
private ZonedDateTime zonedDateTime;

@ExcelColumn(columnIndex = 4)
private BigDecimal ruzgar;
private Instant instant;

@ExcelColumn(columnIndex = 5)
private BigDecimal linyit;
private LocalDateTime localDateTime;

@ExcelColumn(columnIndex = 6)
private BigDecimal taskomur;
private LocalDate localDate;

@ExcelColumn(columnIndex = 7)
private BigDecimal ithalkomur;
private Long longg;

@ExcelColumn(columnIndex = 8)
private BigDecimal fueloil;
private int intt;

@ExcelColumn(columnIndex = 9)
private BigDecimal jeotermal;
@ExcelColumn(columnIndex = 10)
private BigDecimal barajli;
@ExcelColumn(columnIndex = 11)
private BigDecimal nafta;
@ExcelColumn(columnIndex = 12)
private BigDecimal biyokutle;
@ExcelColumn(columnIndex = 13)
private BigDecimal akarsu;
@ExcelColumn(columnIndex = 14)
private BigDecimal diger;
private Integer integer;

}
10 changes: 2 additions & 8 deletions src/main/java/com/kadioglumf/service/ReaderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@
import java.util.List;

@Service
public class ReaderService<T> {
public class ReaderService {

private final Class<T> clazz;

public ReaderService(Class<T> clazz) {
this.clazz = clazz;
}

public List<T> readFile(@NonNull InputStream inputStream, @NonNull String fileExtension, @NonNull boolean isFirstRowHeader) throws Exception {
public <T> List<T> readFile(@NonNull Class<T> clazz, @NonNull InputStream inputStream, @NonNull String fileExtension, @NonNull boolean isFirstRowHeader) throws Exception {
if (FileExtension.XLS.getValue().equals(fileExtension)
|| FileExtension.XLSX.getValue().equals(fileExtension)) {
ExcelReaderUtil readerUtil = new ExcelReaderUtil(inputStream, FileExtension.getFileExtensionByValue(fileExtension));
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/kadioglumf/util/excel/ExcelReaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ else if (isCellDateFormatted(field.getType())) {
}

private boolean isCellNumericFormatted(Class<?> type) {
return Number.class.isAssignableFrom(type);
return Number.class.isAssignableFrom(type)
|| type == int.class || type == double.class
|| type == float.class || type == long.class
|| type == short.class || type == byte.class;
}

private Object parseNumericValue(String cellValue, Class<?> targetType) throws ParseException {
Expand Down
Binary file added src/main/resources/test.xlsx
Binary file not shown.

0 comments on commit ff2fabe

Please sign in to comment.