|
|
@ -117,14 +117,11 @@ public class MultiSheetExcelUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void setCellValue(Cell cell, Object value, FieldMeta meta) {
|
|
|
|
private void setCellValue(Cell cell, Object value, FieldMeta meta) {
|
|
|
|
if (value == null) {
|
|
|
|
if (value == null) {
|
|
|
|
cell.setCellValue("");
|
|
|
|
cell.setCellValue("");
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理日期类型
|
|
|
|
// 处理日期类型
|
|
|
|
if (value instanceof Date) {
|
|
|
|
if (value instanceof Date) {
|
|
|
|
String format = StringUtils.isNotEmpty(meta.getDateFormat())
|
|
|
|
String format = StringUtils.isNotEmpty(meta.getDateFormat())
|
|
|
@ -183,12 +180,18 @@ public class MultiSheetExcelUtil {
|
|
|
|
private CellStyle createDataStyle(Workbook workbook) {
|
|
|
|
private CellStyle createDataStyle(Workbook workbook) {
|
|
|
|
CellStyle style = workbook.createCellStyle();
|
|
|
|
CellStyle style = workbook.createCellStyle();
|
|
|
|
// 设置边框
|
|
|
|
// 设置边框
|
|
|
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
// 设置自动换行(可选)
|
|
|
|
// 设置自动换行(可选)
|
|
|
|
style.setWrapText(true);
|
|
|
|
//style.setWrapText(true);
|
|
|
|
return style;
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -207,16 +210,17 @@ public class MultiSheetExcelUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T> void fillDataRows(Sheet sheet, List<T> list, List<FieldMeta> fields) {
|
|
|
|
private <T> void fillDataRows(Sheet sheet, List<T> list, List<FieldMeta> fields) {
|
|
|
|
|
|
|
|
CellStyle dataStyle = createDataStyle(workbook);
|
|
|
|
int rowNum = 1;
|
|
|
|
int rowNum = 1;
|
|
|
|
for (T item : list) {
|
|
|
|
for (T item : list) {
|
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
FieldMeta meta = fields.get(i);
|
|
|
|
FieldMeta meta = fields.get(i);
|
|
|
|
Cell cell = row.createCell(i);
|
|
|
|
Cell cell = row.createCell(i);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// 直接通过缓存的Field对象获取值
|
|
|
|
// 直接通过缓存的Field对象获取值
|
|
|
|
Object value = meta.getField().get(item);
|
|
|
|
Object value = meta.getField().get(item);
|
|
|
|
|
|
|
|
cell.setCellStyle(dataStyle);
|
|
|
|
setCellValue(cell, value, meta); // 传递FieldMeta对象
|
|
|
|
setCellValue(cell, value, meta); // 传递FieldMeta对象
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
cell.setCellValue("N/A");
|
|
|
|
cell.setCellValue("N/A");
|
|
|
@ -226,26 +230,4 @@ public class MultiSheetExcelUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*private <T> void fillDataRows(Sheet sheet, List<T> list, List<Object[]> fields) {
|
|
|
|
|
|
|
|
CellStyle dataStyle = createDataStyle(workbook);
|
|
|
|
|
|
|
|
int rowNum = 1;
|
|
|
|
|
|
|
|
for (T item : list) {
|
|
|
|
|
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
|
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
|
|
|
|
Cell cell = row.createCell(i);
|
|
|
|
|
|
|
|
Object[] fieldInfo = fields.get(i);
|
|
|
|
|
|
|
|
String fieldName = (String) fieldInfo[0];
|
|
|
|
|
|
|
|
Object value = getFieldValueByName(fieldName, item);
|
|
|
|
|
|
|
|
// 获取Field对象并传递
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Field field = item.getClass().getDeclaredField(fieldName);
|
|
|
|
|
|
|
|
setCellValue(cell, value, field); // 调用新版方法
|
|
|
|
|
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
|
|
|
|
setCellValue(cell, value, null); // 降级处理
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.setCellStyle(dataStyle);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|