|
|
|
|
@ -220,6 +220,37 @@ public abstract class BaseSxssfExcelFile implements ExcelFile {
|
|
|
|
|
short decDf = workbook.createDataFormat().getFormat("#,##0.##");
|
|
|
|
|
decimalNumberStyle.setDataFormat(decDf);
|
|
|
|
|
|
|
|
|
|
// 한글 중요 주석: 정렬 옵션 지원을 위해 기본/숫자 스타일에 대해 좌/중앙/우 정렬 변형을 미리 생성해 재사용한다.
|
|
|
|
|
CellStyle baseLeft = workbook.createCellStyle();
|
|
|
|
|
baseLeft.cloneStyleFrom(dataStyle);
|
|
|
|
|
baseLeft.setAlignment(HorizontalAlignment.LEFT);
|
|
|
|
|
CellStyle baseCenter = workbook.createCellStyle();
|
|
|
|
|
baseCenter.cloneStyleFrom(dataStyle);
|
|
|
|
|
baseCenter.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
CellStyle baseRight = workbook.createCellStyle();
|
|
|
|
|
baseRight.cloneStyleFrom(dataStyle);
|
|
|
|
|
baseRight.setAlignment(HorizontalAlignment.RIGHT);
|
|
|
|
|
|
|
|
|
|
CellStyle intLeft = workbook.createCellStyle();
|
|
|
|
|
intLeft.cloneStyleFrom(integerNumberStyle);
|
|
|
|
|
intLeft.setAlignment(HorizontalAlignment.LEFT);
|
|
|
|
|
CellStyle intCenter = workbook.createCellStyle();
|
|
|
|
|
intCenter.cloneStyleFrom(integerNumberStyle);
|
|
|
|
|
intCenter.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
CellStyle intRight = workbook.createCellStyle();
|
|
|
|
|
intRight.cloneStyleFrom(integerNumberStyle);
|
|
|
|
|
intRight.setAlignment(HorizontalAlignment.RIGHT);
|
|
|
|
|
|
|
|
|
|
CellStyle decLeft = workbook.createCellStyle();
|
|
|
|
|
decLeft.cloneStyleFrom(decimalNumberStyle);
|
|
|
|
|
decLeft.setAlignment(HorizontalAlignment.LEFT);
|
|
|
|
|
CellStyle decCenter = workbook.createCellStyle();
|
|
|
|
|
decCenter.cloneStyleFrom(decimalNumberStyle);
|
|
|
|
|
decCenter.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
CellStyle decRight = workbook.createCellStyle();
|
|
|
|
|
decRight.cloneStyleFrom(decimalNumberStyle);
|
|
|
|
|
decRight.setAlignment(HorizontalAlignment.RIGHT);
|
|
|
|
|
|
|
|
|
|
// 데이터 시작 행 (제목이 있으면 row 2, 없으면 row 1)
|
|
|
|
|
int rowIndex = ROW_START_INDEX + titleRowOffset + 1;
|
|
|
|
|
List<Field> fields = getAllFieldsWithExcelColumn(data.getType());
|
|
|
|
|
@ -227,7 +258,10 @@ public abstract class BaseSxssfExcelFile implements ExcelFile {
|
|
|
|
|
// 각 데이터 객체를 행으로 변환
|
|
|
|
|
for (Object record : data.getDataList()) {
|
|
|
|
|
Row row = sheet.createRow(rowIndex++);
|
|
|
|
|
renderDataRow(row, record, fields, dataStyle, integerNumberStyle, decimalNumberStyle);
|
|
|
|
|
renderDataRow(row, record, fields, dataStyle, integerNumberStyle, decimalNumberStyle,
|
|
|
|
|
baseLeft, baseCenter, baseRight,
|
|
|
|
|
intLeft, intCenter, intRight,
|
|
|
|
|
decLeft, decCenter, decRight);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 컬럼 너비 조정
|
|
|
|
|
@ -310,7 +344,14 @@ public abstract class BaseSxssfExcelFile implements ExcelFile {
|
|
|
|
|
* @param decimalNumberStyle 소수형 숫자 서식 스타일(#,##0.##)
|
|
|
|
|
* @throws RuntimeException 필드 접근 실패 시
|
|
|
|
|
*/
|
|
|
|
|
private void renderDataRow(Row row, Object record, List<Field> fields, CellStyle baseStyle, CellStyle integerNumberStyle, CellStyle decimalNumberStyle) {
|
|
|
|
|
private void renderDataRow(
|
|
|
|
|
Row row,
|
|
|
|
|
Object record,
|
|
|
|
|
List<Field> fields,
|
|
|
|
|
CellStyle baseStyle, CellStyle integerNumberStyle, CellStyle decimalNumberStyle,
|
|
|
|
|
CellStyle baseLeft, CellStyle baseCenter, CellStyle baseRight,
|
|
|
|
|
CellStyle intLeft, CellStyle intCenter, CellStyle intRight,
|
|
|
|
|
CellStyle decLeft, CellStyle decCenter, CellStyle decRight) {
|
|
|
|
|
int columnIndex = COLUMN_START_INDEX;
|
|
|
|
|
try {
|
|
|
|
|
for (Field field : fields) {
|
|
|
|
|
@ -320,6 +361,7 @@ public abstract class BaseSxssfExcelFile implements ExcelFile {
|
|
|
|
|
|
|
|
|
|
// 수식 처리: ExcelColumn에 formula 설정이 있는 경우 수식을 생성하여 설정
|
|
|
|
|
ExcelColumn excelColumn = field.getAnnotation(ExcelColumn.class);
|
|
|
|
|
ExcelColumn.Align align = (excelColumn != null) ? excelColumn.align() : ExcelColumn.Align.AUTO;
|
|
|
|
|
if (excelColumn != null && excelColumn.formula() && !excelColumn.formulaRefField().isEmpty()) {
|
|
|
|
|
String refFieldName = excelColumn.formulaRefField();
|
|
|
|
|
int refColumnIndex = findFieldColumnIndex(fields, refFieldName);
|
|
|
|
|
@ -329,10 +371,14 @@ public abstract class BaseSxssfExcelFile implements ExcelFile {
|
|
|
|
|
refField.setAccessible(true);
|
|
|
|
|
Object refValue = refField.get(record);
|
|
|
|
|
boolean isBlankRef = (refValue == null) || (refValue instanceof String && ((String) refValue).trim().isEmpty());
|
|
|
|
|
CellStyle baseAligned = baseStyle;
|
|
|
|
|
if (align == ExcelColumn.Align.LEFT) baseAligned = baseLeft;
|
|
|
|
|
else if (align == ExcelColumn.Align.CENTER) baseAligned = baseCenter;
|
|
|
|
|
else if (align == ExcelColumn.Align.RIGHT) baseAligned = baseRight;
|
|
|
|
|
if (isBlankRef) {
|
|
|
|
|
Cell cell = row.createCell(columnIndex++);
|
|
|
|
|
cell.setCellValue("");
|
|
|
|
|
cell.setCellStyle(baseStyle);
|
|
|
|
|
cell.setCellStyle(baseAligned);
|
|
|
|
|
continue; // 다음 필드 처리
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -342,18 +388,35 @@ public abstract class BaseSxssfExcelFile implements ExcelFile {
|
|
|
|
|
|
|
|
|
|
Cell cell = row.createCell(columnIndex++);
|
|
|
|
|
cell.setCellFormula(formula);
|
|
|
|
|
cell.setCellStyle(baseStyle);
|
|
|
|
|
cell.setCellStyle(baseAligned);
|
|
|
|
|
continue; // 다음 필드 처리
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 기본 값 처리: 숫자 타입일 경우 천단위 콤마 서식 적용
|
|
|
|
|
// 기본 값 처리: 숫자 타입일 경우 천단위 콤마 서식 적용 + 정렬 옵션 반영
|
|
|
|
|
boolean isIntNum = (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long);
|
|
|
|
|
boolean isDecNum = (value instanceof Float || value instanceof Double || value instanceof BigDecimal);
|
|
|
|
|
CellStyle styleToUse = baseStyle;
|
|
|
|
|
if (value instanceof Byte || value instanceof Short || value instanceof Integer || value instanceof Long) {
|
|
|
|
|
if (isIntNum) {
|
|
|
|
|
styleToUse = integerNumberStyle;
|
|
|
|
|
} else if (value instanceof Float || value instanceof Double || value instanceof BigDecimal) {
|
|
|
|
|
} else if (isDecNum) {
|
|
|
|
|
styleToUse = decimalNumberStyle;
|
|
|
|
|
}
|
|
|
|
|
if (align != ExcelColumn.Align.AUTO) {
|
|
|
|
|
if (isIntNum) {
|
|
|
|
|
if (align == ExcelColumn.Align.LEFT) styleToUse = intLeft;
|
|
|
|
|
else if (align == ExcelColumn.Align.CENTER) styleToUse = intCenter;
|
|
|
|
|
else if (align == ExcelColumn.Align.RIGHT) styleToUse = intRight;
|
|
|
|
|
} else if (isDecNum) {
|
|
|
|
|
if (align == ExcelColumn.Align.LEFT) styleToUse = decLeft;
|
|
|
|
|
else if (align == ExcelColumn.Align.CENTER) styleToUse = decCenter;
|
|
|
|
|
else if (align == ExcelColumn.Align.RIGHT) styleToUse = decRight;
|
|
|
|
|
} else {
|
|
|
|
|
if (align == ExcelColumn.Align.LEFT) styleToUse = baseLeft;
|
|
|
|
|
else if (align == ExcelColumn.Align.CENTER) styleToUse = baseCenter;
|
|
|
|
|
else if (align == ExcelColumn.Align.RIGHT) styleToUse = baseRight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
createCell(row, columnIndex++, value, styleToUse);
|
|
|
|
|
}
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
|