-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
824 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.poiji.save; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.time.ZonedDateTime; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.BiConsumer; | ||
import org.apache.poi.hpsf.CustomProperties; | ||
import org.apache.poi.hssf.usermodel.HSSFWorkbook; | ||
import org.apache.poi.ooxml.POIXMLProperties; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.apache.poi.xssf.streaming.SXSSFWorkbook; | ||
|
||
import static com.poiji.util.PoijiConstants.DATE_CELL_STYLE_INDEX_PROPERTY_NAME; | ||
import static com.poiji.util.PoijiConstants.DATE_TIME_CELL_STYLE_INDEX_PROPERTY_NAME; | ||
|
||
public final class CellDataProvider { | ||
|
||
private final Map<Class<?>, BiConsumer<Cell, Object>> consumers; | ||
|
||
public CellDataProvider() { | ||
consumers = new ConcurrentHashMap<>(); | ||
consumers.put(Boolean.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((boolean) fieldValue); | ||
} | ||
}); | ||
consumers.put(boolean.class, (Cell cell, Object fieldValue) -> cell.setCellValue((boolean) fieldValue)); | ||
consumers.put(Double.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((double) fieldValue); | ||
} | ||
}); | ||
consumers.put(double.class, (Cell cell, Object fieldValue) -> cell.setCellValue((double) fieldValue)); | ||
consumers.put(Long.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((long) fieldValue); | ||
} | ||
}); | ||
consumers.put(long.class, (Cell cell, Object fieldValue) -> cell.setCellValue((long) fieldValue)); | ||
consumers.put(Integer.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((int) fieldValue); | ||
} | ||
}); | ||
consumers.put(int.class, (Cell cell, Object fieldValue) -> cell.setCellValue((int) fieldValue)); | ||
consumers.put(Byte.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((byte) fieldValue); | ||
} | ||
}); | ||
consumers.put(byte.class, (Cell cell, Object fieldValue) -> cell.setCellValue((byte) fieldValue)); | ||
consumers.put(Short.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((short) fieldValue); | ||
} | ||
}); | ||
consumers.put(short.class, (Cell cell, Object fieldValue) -> cell.setCellValue((short) fieldValue)); | ||
consumers.put(Float.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((float) fieldValue); | ||
} | ||
}); | ||
consumers.put(float.class, (Cell cell, Object fieldValue) -> cell.setCellValue((float) fieldValue)); | ||
consumers.put(java.util.Date.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((java.util.Date) fieldValue); | ||
setStyleInCell(cell, DATE_TIME_CELL_STYLE_INDEX_PROPERTY_NAME); | ||
} | ||
}); | ||
consumers.put(java.sql.Date.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((java.util.Date) fieldValue); | ||
setStyleInCell(cell, DATE_TIME_CELL_STYLE_INDEX_PROPERTY_NAME); | ||
} | ||
}); | ||
consumers.put(Calendar.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue((Calendar) fieldValue); | ||
setStyleInCell(cell, DATE_TIME_CELL_STYLE_INDEX_PROPERTY_NAME); | ||
} | ||
}); | ||
consumers.put(LocalDateTime.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue(java.sql.Timestamp.valueOf((LocalDateTime) fieldValue)); | ||
setStyleInCell(cell, DATE_TIME_CELL_STYLE_INDEX_PROPERTY_NAME); | ||
} | ||
}); | ||
consumers.put(ZonedDateTime.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue(Date.from(Instant.from((TemporalAccessor) fieldValue))); | ||
setStyleInCell(cell, DATE_TIME_CELL_STYLE_INDEX_PROPERTY_NAME); | ||
} | ||
}); | ||
consumers.put(LocalDate.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue(java.sql.Date.valueOf((LocalDate) fieldValue)); | ||
setStyleInCell(cell, DATE_CELL_STYLE_INDEX_PROPERTY_NAME); | ||
} | ||
}); | ||
consumers.put(Object.class, (Cell cell, Object fieldValue) -> { | ||
if (fieldValue != null) { | ||
cell.setCellValue(fieldValue.toString()); | ||
} | ||
}); | ||
} | ||
|
||
private void setStyleInCell(final Cell cell, final String propertyName) { | ||
final Workbook workbook = cell.getSheet().getWorkbook(); | ||
if (workbook instanceof SXSSFWorkbook) { | ||
final POIXMLProperties.CustomProperties properties = ((SXSSFWorkbook) workbook) | ||
.getXSSFWorkbook() | ||
.getProperties() | ||
.getCustomProperties(); | ||
final int dateTimeCellStyleIndex = properties.getProperty(propertyName).getI4(); | ||
cell.setCellStyle(workbook.getCellStyleAt(dateTimeCellStyleIndex)); | ||
} else if (workbook instanceof HSSFWorkbook) { | ||
final CustomProperties properties = ((HSSFWorkbook) workbook) | ||
.getDocumentSummaryInformation() | ||
.getCustomProperties(); | ||
final Short dateTimeCellStyleIndex = (Short) properties.get(propertyName); | ||
cell.setCellStyle(workbook.getCellStyleAt(dateTimeCellStyleIndex)); | ||
} else { | ||
throw new UnsupportedOperationException(workbook.getClass() + " is not supported"); | ||
} | ||
} | ||
|
||
public BiConsumer<Cell, Object> forType(final Class<?> type) { | ||
return consumers.getOrDefault(type, consumers.get(Object.class)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.poiji.save; | ||
|
||
import java.util.List; | ||
|
||
public interface FileSaver { | ||
|
||
<T> void save(List<T> data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.poiji.save; | ||
|
||
import com.poiji.exception.InvalidExcelFileExtension; | ||
import com.poiji.option.PoijiOptions; | ||
import java.io.File; | ||
|
||
import static com.poiji.util.PoijiConstants.XLSX_EXTENSION; | ||
import static com.poiji.util.PoijiConstants.XLS_EXTENSION; | ||
|
||
public final class FileSaverFactory<T> { | ||
|
||
private final Class<T> entity; | ||
private final PoijiOptions options; | ||
|
||
public FileSaverFactory(final Class<T> entity, final PoijiOptions options) { | ||
this.entity = entity; | ||
this.options = options; | ||
} | ||
|
||
public FileSaver toFile(final File file) { | ||
final MappedFields mappedFields = new MappedFields(entity).parseEntity(); | ||
if (file.toString().endsWith(XLSX_EXTENSION)) { | ||
return new XlsxFileSaver(file, mappedFields, options); | ||
} else if (file.toString().endsWith(XLS_EXTENSION)) { | ||
return new XlsFileSaver(file, mappedFields, options); | ||
} else { | ||
throw new InvalidExcelFileExtension(file.getName() + " has unsupported extension. 'xlsx' and 'xls' are supported only."); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.poiji.save; | ||
|
||
import com.poiji.exception.PoijiException; | ||
import com.poiji.option.PoijiOptions; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
|
||
public class FileWorkbookSaver extends WorkbookSaver { | ||
|
||
private final File file; | ||
|
||
public FileWorkbookSaver( | ||
final File file, final MappedFields mappedFields, final PoijiOptions options | ||
) { | ||
super(mappedFields, options); | ||
this.file = file; | ||
} | ||
|
||
protected <T> void save(final List<T> data, final Workbook workbook) { | ||
createFile(); | ||
writeInFile(data, workbook); | ||
} | ||
|
||
private <T> void writeInFile(final List<T> data, final Workbook workbook) { | ||
try (final FileOutputStream outputStream = new FileOutputStream(file)) { | ||
save(data, workbook, outputStream); | ||
} catch (IOException e) { | ||
throw new PoijiException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
private void createFile() { | ||
final Path absolutePath = file.toPath().toAbsolutePath(); | ||
try { | ||
Files.createDirectories(absolutePath.getParent()); | ||
Files.deleteIfExists(absolutePath); | ||
Files.createFile(absolutePath); | ||
} catch (IOException e) { | ||
throw new PoijiException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.