You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.7 KiB
Java

package egovframework.util.excel;
import org.apache.poi.ss.usermodel.*;
import java.io.IOException;
import java.io.OutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public interface ExcelFile { // (1)
void write(OutputStream stream) throws IOException;
void writeWithEncryption(OutputStream stream, String password) throws IOException;
default <T> void createCell(Row row, int column, T value, CellStyle style) {
if (value == null) {
return; // avoid NPE
}
Cell cell = row.createCell(column);
if (value instanceof Integer) {
cell.setCellValue((Integer)value);
} else if (value instanceof Long) {
cell.setCellValue((Long)value);
} else if (value instanceof Double) {
cell.setCellValue((Double)value);
} else if (value instanceof Float) {
cell.setCellValue((Float)value);
} else if (value instanceof Boolean) {
cell.setCellValue((Boolean)value);
} else if (value instanceof LocalDateTime) {
LocalDateTime dateTime = (LocalDateTime)value;
cell.setCellValue(dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
} else if (value instanceof LocalDate) {
LocalDate date = (LocalDate)value;
cell.setCellValue(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
} else if (value instanceof LocalTime) {
LocalTime time = (LocalTime)value;
cell.setCellValue(time.format(DateTimeFormatter.ofPattern("HH:mm:ss")));
} else {
cell.setCellValue(value.toString());
}
cell.setCellStyle(style);
}
default CellStyle createCellStyle(Workbook wb, boolean isBold) {
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setBold(isBold);
style.setFont(font);
return style;
}
}