|
@@ -17,7 +17,6 @@ import com.boman.common.core.utils.reflect.ReflectUtils;
|
|
|
import com.boman.domain.GenTableColumn;
|
|
|
import com.boman.domain.SysDictData;
|
|
|
import com.boman.domain.constant.FormDataConstant;
|
|
|
-import com.boman.domain.constant.MaskConstant;
|
|
|
import org.apache.commons.lang3.BooleanUtils;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
|
@@ -397,19 +396,19 @@ public class ExcelUtil<T>
|
|
|
/**
|
|
|
* 功能描述: 通用的导出
|
|
|
*
|
|
|
- * @param response 返回数据
|
|
|
- * @param list 导出数据集合
|
|
|
- * @param sheetName 工作表的名称
|
|
|
- * @param columns 所有的列
|
|
|
- * @param empty 所有的列
|
|
|
- * @return void
|
|
|
+ * @param response 返回数据
|
|
|
+ * @param list 导出数据集合
|
|
|
+ * @param sheetName 工作表的名称
|
|
|
+ * @param listVisibleColumns 列表可见
|
|
|
+ * @param empty 所有的列
|
|
|
+ * @param result true直接导出结果 false需要检查列...
|
|
|
*/
|
|
|
public void exportExcelCommon(HttpServletResponse response, List<Map<String, Object>> list
|
|
|
- , String sheetName, List<GenTableColumn> columns, Boolean empty) throws IOException {
|
|
|
+ , String sheetName, List<GenTableColumn> listVisibleColumns, Boolean empty, Boolean result) throws IOException {
|
|
|
response.setContentType("application/vnd.ms-excel");
|
|
|
response.setCharacterEncoding("utf-8");
|
|
|
- this.initJSONObject(list, sheetName, Type.EXPORT);
|
|
|
- exportExcelCommon(response.getOutputStream(), list, columns, empty);
|
|
|
+ initJSONObject(list, sheetName, Type.EXPORT);
|
|
|
+ exportExcelCommon(response.getOutputStream(), list, listVisibleColumns, empty, result);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -433,16 +432,15 @@ public class ExcelUtil<T>
|
|
|
/**
|
|
|
* 功能描述: 通用导出
|
|
|
*
|
|
|
- * @param outputStream outputStream
|
|
|
- * @param columns columns
|
|
|
- * @param empty true=>只带表头的excel,不含数据, false=>带数据的excel
|
|
|
- * @return void
|
|
|
+ * @param outputStream outputStream
|
|
|
+ * @param listVisibleColumns listVisibleColumns
|
|
|
+ * @param empty true=>只带表头的excel,不含数据, false=>带数据的excel
|
|
|
+ * @param result true直接导出结果, false需要检查列...
|
|
|
*/
|
|
|
- public void exportExcelCommon(OutputStream outputStream, List<Map<String, Object>> list, List<GenTableColumn> columns, Boolean empty) {
|
|
|
+ public void exportExcelCommon(OutputStream outputStream, List<Map<String, Object>> list, List<GenTableColumn> listVisibleColumns
|
|
|
+ , Boolean empty, Boolean result) {
|
|
|
try {
|
|
|
- // 写入各个字段的列头名称 列表可见
|
|
|
- List<GenTableColumn> listVisibleColumns = filterData(columns, 4, MaskConstant.LIST_VISIBLE::equals);
|
|
|
-
|
|
|
+// list = sortMapByKey(list);
|
|
|
// 取出一共有多少个sheet.
|
|
|
double sheetNo = Math.ceil(list.size() / sheetSize);
|
|
|
for (int index = 0; index <= sheetNo; index++) {
|
|
@@ -451,13 +449,26 @@ public class ExcelUtil<T>
|
|
|
// 产生一行
|
|
|
Row row = sheet.createRow(0);
|
|
|
int columnIndex = 0;
|
|
|
- for (GenTableColumn column : listVisibleColumns) {
|
|
|
- createCellByColumn(column, row, columnIndex++);
|
|
|
- }
|
|
|
|
|
|
- if (BooleanUtils.isFalse(empty)) {
|
|
|
- if (Type.EXPORT.equals(type)) {
|
|
|
- fillExcelDataByJSONObject(index, row, listVisibleColumns);
|
|
|
+ // 直接导出结果,无需检查列
|
|
|
+ if (BooleanUtils.isTrue(result)) {
|
|
|
+ Map<String, Object> map = list.get(0);
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
+ String columnName = entry.getKey();
|
|
|
+ columnName = covertHead(columnName);
|
|
|
+ createCellByMap(columnName, row, columnIndex++);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (BooleanUtils.isFalse(empty) && Type.EXPORT.equals(type)) {
|
|
|
+ fillDataByListMap(index);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (GenTableColumn column : listVisibleColumns) {
|
|
|
+ createCellByColumn(column, row, columnIndex++);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (BooleanUtils.isFalse(empty) && Type.EXPORT.equals(type)) {
|
|
|
+ fillDataByColumns(index, row, listVisibleColumns);
|
|
|
// addStatisticsRow();
|
|
|
}
|
|
|
}
|
|
@@ -467,19 +478,42 @@ public class ExcelUtil<T>
|
|
|
} catch (Exception e) {
|
|
|
log.error("导出Excel异常{}", e.getMessage());
|
|
|
} finally {
|
|
|
- if (wb != null) {
|
|
|
- try {
|
|
|
- wb.close();
|
|
|
- } catch (IOException e1) {
|
|
|
- e1.printStackTrace();
|
|
|
- }
|
|
|
+ close(outputStream);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String covertHead(String columnName) {
|
|
|
+ switch (columnName){
|
|
|
+ case "userName":
|
|
|
+ return "用户名";
|
|
|
+ case "deptName":
|
|
|
+ return "部门名称";
|
|
|
+ case "date":
|
|
|
+ return "日期";
|
|
|
+ case "attendanceTableLeaveSum":
|
|
|
+ return "早退";
|
|
|
+ case "attendanceTableLeaveOrSum":
|
|
|
+ return "请假";
|
|
|
+ case "attendanceTableLateSum":
|
|
|
+ return "迟到";
|
|
|
+ default:
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void close(OutputStream outputStream) {
|
|
|
+ if (wb != null) {
|
|
|
+ try {
|
|
|
+ wb.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
}
|
|
|
- if (outputStream != null) {
|
|
|
- try {
|
|
|
- outputStream.close();
|
|
|
- } catch (IOException e1) {
|
|
|
- e1.printStackTrace();
|
|
|
- }
|
|
|
+ }
|
|
|
+ if (outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -536,28 +570,7 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
- if (wb != null)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- wb.close();
|
|
|
- }
|
|
|
- catch (IOException e1)
|
|
|
- {
|
|
|
- e1.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- if (outputStream != null)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- outputStream.close();
|
|
|
- }
|
|
|
- catch (IOException e1)
|
|
|
- {
|
|
|
- e1.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
+ close(outputStream);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -595,14 +608,30 @@ public class ExcelUtil<T>
|
|
|
* @param index 序号
|
|
|
* @param row 单元格行
|
|
|
*/
|
|
|
- public void fillExcelDataByJSONObject(int index, Row row, List<GenTableColumn> listVisibleColumns) {
|
|
|
+ public void fillDataByColumns(int index, Row row, List<GenTableColumn> listVisibleColumns) {
|
|
|
int startNo = index * sheetSize;
|
|
|
int endNo = Math.min(startNo + sheetSize, jsonObjectList.size());
|
|
|
for (int i = startNo; i < endNo; i++) {
|
|
|
row = sheet.createRow(i + 1 - startNo);
|
|
|
Map<String, Object> jsonObject = jsonObjectList.get(i);
|
|
|
|
|
|
- addCellByJSONObject(row, jsonObject, listVisibleColumns);
|
|
|
+ addCellByColumns(row, jsonObject, listVisibleColumns);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 填充excel数据
|
|
|
+ * @param index 序号
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void fillDataByListMap(int index) {
|
|
|
+ int startNo = index * sheetSize;
|
|
|
+ int endNo = Math.min(startNo + sheetSize, jsonObjectList.size());
|
|
|
+ for (int i = startNo; i < endNo; i++) {
|
|
|
+ Row row = sheet.createRow(i + 1 - startNo);
|
|
|
+ Map<String, Object> jsonObject = jsonObjectList.get(i);
|
|
|
+ addCellByMap(row, jsonObject);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -715,6 +744,32 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建单元格
|
|
|
+ */
|
|
|
+ public void createCellByMap(String columnComment, Row row, int columnIndex) {
|
|
|
+ // 创建列
|
|
|
+ Cell cell = row.createCell(columnIndex);
|
|
|
+ // 写入列信息
|
|
|
+ cell.setCellValue(columnComment);
|
|
|
+ setDataValidationByColumnComment(columnComment, columnIndex);
|
|
|
+
|
|
|
+ CellStyle style = wb.createCellStyle();
|
|
|
+ style.cloneStyleFrom(styles.get("data"));
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+// style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
|
|
|
+// style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ Font headerFont = wb.createFont();
|
|
|
+ headerFont.setFontName("Arial");
|
|
|
+ headerFont.setFontHeightInPoints((short) 15);
|
|
|
+ headerFont.setBold(true);
|
|
|
+// headerFont.setColor(IndexedColors.RED.getIndex());
|
|
|
+ style.setFont(headerFont);
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设置单元格信息
|
|
|
*
|
|
@@ -813,8 +868,19 @@ public class ExcelUtil<T>
|
|
|
// 设置列宽
|
|
|
sheet.setColumnWidth(columnIndex, (int) ((16 + 0.72) * 256));
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 创建表格样式
|
|
|
+ */
|
|
|
+ public void setDataValidationByColumnComment(String columnComment, int columnIndex) {
|
|
|
+ if (columnComment.contains("注:")) {
|
|
|
+ sheet.setColumnWidth(columnIndex, 6000);
|
|
|
+ } else {
|
|
|
+ // 设置列宽
|
|
|
+ sheet.setColumnWidth(columnIndex, (int) ((16 + 0.72) * 256));
|
|
|
+ }
|
|
|
+ }
|
|
|
// 如果设置了提示信息则鼠标放上去提示.
|
|
|
// if (StringUtils.isNotEmpty(attr.prompt())) {
|
|
|
// // 这里默认设了2-101列提示.
|
|
@@ -825,12 +891,12 @@ public class ExcelUtil<T>
|
|
|
// // 这里默认设了2-101列只能选择不能输入.
|
|
|
// setXSSFValidation(sheet, attr.combo(), 1, 100, columnIndex, columnIndex);
|
|
|
// }
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 添加单元格
|
|
|
*/
|
|
|
- public void addCellByJSONObject(Row row, Map<String, Object> jsonObject, List<GenTableColumn> listVisibleColumns) {
|
|
|
+ public void addCellByColumns(Row row, Map<String, Object> jsonObject, List<GenTableColumn> listVisibleColumns) {
|
|
|
Cell cell;
|
|
|
try {
|
|
|
int columnIndex = 0;
|
|
@@ -897,6 +963,27 @@ public class ExcelUtil<T>
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 添加单元格
|
|
|
+ */
|
|
|
+ public void addCellByMap(Row row, Map<String, Object> jsonObject) {
|
|
|
+ Cell cell;
|
|
|
+ try {
|
|
|
+ int columnIndex = 0;
|
|
|
+ for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
|
|
|
+ Object value = entry.getValue();
|
|
|
+ // 设置行高
|
|
|
+ row.setHeight((short) 600);
|
|
|
+ // 创建cell
|
|
|
+ cell = row.createCell(columnIndex++);
|
|
|
+ cell.setCellStyle(styles.get("data"));
|
|
|
+ cell.setCellValue(String.valueOf(value));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导出Excel失败: {}", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 添加单元格
|
|
|
*/
|
|
@@ -1423,4 +1510,25 @@ public class ExcelUtil<T>
|
|
|
return sysDictData.getDictLabel();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 使用 Map按key进行排序
|
|
|
+ *
|
|
|
+ * @param mapList
|
|
|
+ * @return mapList
|
|
|
+ */
|
|
|
+ public static List<Map<String, Object>> sortMapByKey(List<Map<String, Object>> mapList) {
|
|
|
+ if (mapList == null || mapList.isEmpty()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>(mapList.size());
|
|
|
+ for (Map<String, Object> map : mapList) {
|
|
|
+ Map<String, Object> sortMap = new TreeMap<>(map);
|
|
|
+
|
|
|
+ result.add(sortMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|