|
@@ -1,60 +1,49 @@
|
|
|
package com.boman.common.core.utils.poi;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.util.TypeUtils;
|
|
|
+import com.boman.domain.annotation.Excel;
|
|
|
+import com.boman.domain.annotation.Excel.ColumnType;
|
|
|
+import com.boman.domain.annotation.Excel.Type;
|
|
|
+import com.boman.domain.annotation.Excels;
|
|
|
+import com.boman.common.core.text.Convert;
|
|
|
+import com.boman.common.core.utils.DateUtils;
|
|
|
+import com.boman.common.core.utils.StringUtils;
|
|
|
+import com.boman.common.core.utils.array.ArrayUtils;
|
|
|
+import com.boman.common.core.utils.file.FileTypeUtils;
|
|
|
+import com.boman.common.core.utils.file.ImageUtils;
|
|
|
+import com.boman.common.core.utils.obj.ObjectUtils;
|
|
|
+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 org.apache.commons.lang3.BooleanUtils;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.text.DecimalFormat;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.Comparator;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Predicate;
|
|
|
import java.util.stream.Collectors;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import org.apache.poi.ss.usermodel.BorderStyle;
|
|
|
-import org.apache.poi.ss.usermodel.Cell;
|
|
|
-import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
-import org.apache.poi.ss.usermodel.CellType;
|
|
|
-import org.apache.poi.ss.usermodel.ClientAnchor;
|
|
|
-import org.apache.poi.ss.usermodel.DataValidation;
|
|
|
-import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
|
|
-import org.apache.poi.ss.usermodel.DataValidationHelper;
|
|
|
-import org.apache.poi.ss.usermodel.DateUtil;
|
|
|
-import org.apache.poi.ss.usermodel.Drawing;
|
|
|
-import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
-import org.apache.poi.ss.usermodel.Font;
|
|
|
-import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
-import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
-import org.apache.poi.ss.usermodel.Row;
|
|
|
-import org.apache.poi.ss.usermodel.Sheet;
|
|
|
-import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
-import org.apache.poi.ss.usermodel.Workbook;
|
|
|
-import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
-import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
-import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
-import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
|
|
|
-import org.apache.poi.xssf.usermodel.XSSFDataValidation;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import com.boman.common.core.annotation.Excel;
|
|
|
-import com.boman.common.core.annotation.Excel.ColumnType;
|
|
|
-import com.boman.common.core.annotation.Excel.Type;
|
|
|
-import com.boman.common.core.annotation.Excels;
|
|
|
-import com.boman.common.core.text.Convert;
|
|
|
-import com.boman.common.core.utils.DateUtils;
|
|
|
-import com.boman.common.core.utils.StringUtils;
|
|
|
-import com.boman.common.core.utils.file.FileTypeUtils;
|
|
|
-import com.boman.common.core.utils.file.ImageUtils;
|
|
|
-import com.boman.common.core.utils.reflect.ReflectUtils;
|
|
|
+
|
|
|
+import static com.boman.common.core.utils.obj.ObjectUtils.*;
|
|
|
+import static com.boman.domain.constant.FormDataConstant.*;
|
|
|
|
|
|
/**
|
|
|
* Excel相关处理
|
|
|
- *
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
public class ExcelUtil<T>
|
|
@@ -96,6 +85,11 @@ public class ExcelUtil<T>
|
|
|
*/
|
|
|
private List<T> list;
|
|
|
|
|
|
+ /**
|
|
|
+ * 导入导出数据列表
|
|
|
+ */
|
|
|
+ private List<Map<String, Object>> jsonObjectList;
|
|
|
+
|
|
|
/**
|
|
|
* 注解列表
|
|
|
*/
|
|
@@ -139,9 +133,18 @@ public class ExcelUtil<T>
|
|
|
createWorkbook();
|
|
|
}
|
|
|
|
|
|
+ public void initJSONObject(List<Map<String, Object>> list, String sheetName, Type type)
|
|
|
+ {
|
|
|
+ this.jsonObjectList = list;
|
|
|
+ this.sheetName = sheetName;
|
|
|
+ this.type = type;
|
|
|
+ createExcelField();
|
|
|
+ createWorkbook();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 对excel表单默认第一个索引名转换成list
|
|
|
- *
|
|
|
+ *
|
|
|
* @param is 输入流
|
|
|
* @return 转换后集合
|
|
|
*/
|
|
@@ -152,7 +155,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 对excel表单指定表格索引名转换成list
|
|
|
- *
|
|
|
+ *
|
|
|
* @param sheetName 表格索引名
|
|
|
* @param is 输入流
|
|
|
* @return 转换后集合
|
|
@@ -184,22 +187,7 @@ public class ExcelUtil<T>
|
|
|
if (rows > 0)
|
|
|
{
|
|
|
// 定义一个map用于存放excel列的序号和field.
|
|
|
- Map<String, Integer> cellMap = new HashMap<String, Integer>();
|
|
|
- // 获取表头
|
|
|
- Row heard = sheet.getRow(0);
|
|
|
- for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++)
|
|
|
- {
|
|
|
- Cell cell = heard.getCell(i);
|
|
|
- if (StringUtils.isNotNull(cell))
|
|
|
- {
|
|
|
- String value = this.getCellValue(heard, i).toString();
|
|
|
- cellMap.put(value, i);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- cellMap.put(null, i);
|
|
|
- }
|
|
|
- }
|
|
|
+ Map<String, Integer> cellMap = getHead(sheet);
|
|
|
// 有数据时才处理 得到类的所有field.
|
|
|
Field[] allFields = clazz.getDeclaredFields();
|
|
|
// 定义一个map用于存放列的序号和field.
|
|
@@ -310,9 +298,123 @@ public class ExcelUtil<T>
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 对excel表单指定表格索引名转换成list
|
|
|
+ *
|
|
|
+ * @param sheetName 表格索引名
|
|
|
+ * @param is 输入流
|
|
|
+ * @return 转换后集合
|
|
|
+ */
|
|
|
+ public List<JSONObject> importCommonExcel(String sheetName, InputStream is, List<GenTableColumn> columns) throws Exception {
|
|
|
+ List<JSONObject> dataList = new ArrayList<>(16);
|
|
|
+ this.type = Type.IMPORT;
|
|
|
+ this.wb = WorkbookFactory.create(is);
|
|
|
+ Sheet sheet = getSheet(sheetName);
|
|
|
+ int rows = sheet.getPhysicalNumberOfRows();
|
|
|
+ if (rows < 0) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Integer> cellMap = getHead(sheet);
|
|
|
+ Map<Integer, GenTableColumn> fieldsMap = new HashMap<>(16);
|
|
|
+ for (GenTableColumn column : columns) {
|
|
|
+ for (Map.Entry<String, Integer> entry : cellMap.entrySet()) {
|
|
|
+ if (entry.getKey().equals(column.getColumnComment())) {
|
|
|
+ Integer sort = cellMap.get(entry.getKey());
|
|
|
+ fieldsMap.put(sort, column);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 往data中赋值
|
|
|
+ for (int i = 1; i < rows; i++) {
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+ JSONObject data = new JSONObject();
|
|
|
+ for (Map.Entry<Integer, GenTableColumn> entry : fieldsMap.entrySet()) {
|
|
|
+ GenTableColumn column = entry.getValue();
|
|
|
+ String columnName = column.getColumnName();
|
|
|
+ Object value = this.getCellValue(row, entry.getKey());
|
|
|
+ value = handleFK(column.getColumnName(), value, columns);
|
|
|
+ data.put(columnName, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ dataList.add(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ return dataList;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Sheet getSheet(String sheetName) throws IOException {
|
|
|
+ Sheet sheet;
|
|
|
+ if (StringUtils.isNotEmpty(sheetName)) {
|
|
|
+ // 如果指定sheet名,则取指定sheet中的内容.
|
|
|
+ sheet = wb.getSheet(sheetName);
|
|
|
+ } else {
|
|
|
+ // 如果传入的sheet名不存在则默认指向第1个sheet.
|
|
|
+ sheet = wb.getSheetAt(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sheet == null) {
|
|
|
+ throw new IOException("文件sheet不存在");
|
|
|
+ }
|
|
|
+ return sheet;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Integer> getHead(Sheet sheet) {
|
|
|
+ // 定义一个map用于存放excel列的序号和field.
|
|
|
+ Map<String, Integer> cellMap = new HashMap<>();
|
|
|
+ // 获取表头
|
|
|
+ Row heard = sheet.getRow(0);
|
|
|
+ for (int i = 0; i < heard.getPhysicalNumberOfCells(); i++) {
|
|
|
+ Cell cell = heard.getCell(i);
|
|
|
+ if (StringUtils.isNotNull(cell)) {
|
|
|
+ String value = this.getCellValue(heard, i).toString();
|
|
|
+ cellMap.put(value, i);
|
|
|
+ } else {
|
|
|
+ cellMap.put(null, i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return cellMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object handleFK(String columnName, Object value, List<GenTableColumn> columns) {
|
|
|
+ for (GenTableColumn column : columns) {
|
|
|
+ if (columnName.equals(column.getColumnName())) {
|
|
|
+ if (ObjectUtils.isEmpty(column.getDictType())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<SysDictData> dictDataList = column.getSysDictData();
|
|
|
+ SysDictData sysDictData = ObjectUtils.filterOne(dictDataList, dict -> dict.getDictLabel().equals(String.valueOf(value)));
|
|
|
+ return sysDictData.getDictValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 通用的导出
|
|
|
+ *
|
|
|
+ * @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> listVisibleColumns, Boolean empty, Boolean result) throws IOException {
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ initJSONObject(list, sheetName, Type.EXPORT);
|
|
|
+ exportExcelCommon(response.getOutputStream(), list, listVisibleColumns, empty, result);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 对list数据源将其里面的数据导入到excel表单
|
|
|
- *
|
|
|
+ *
|
|
|
* @param response 返回数据
|
|
|
* @param list 导出数据集合
|
|
|
* @param sheetName 工作表的名称
|
|
@@ -327,9 +429,98 @@ public class ExcelUtil<T>
|
|
|
exportExcel(response.getOutputStream());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 功能描述: 通用导出
|
|
|
+ *
|
|
|
+ * @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> listVisibleColumns
|
|
|
+ , Boolean empty, Boolean result) {
|
|
|
+ try {
|
|
|
+// list = sortMapByKey(list);
|
|
|
+ // 取出一共有多少个sheet.
|
|
|
+ double sheetNo = Math.ceil(list.size() / sheetSize);
|
|
|
+ for (int index = 0; index <= sheetNo; index++) {
|
|
|
+ createSheet(sheetNo, index);
|
|
|
+
|
|
|
+ // 产生一行
|
|
|
+ Row row = sheet.createRow(0);
|
|
|
+ int columnIndex = 0;
|
|
|
+
|
|
|
+ // 直接导出结果,无需检查列
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ wb.write(outputStream);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导出Excel异常{}", e.getMessage());
|
|
|
+ } finally {
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 对list数据源将其里面的数据导入到excel表单
|
|
|
- *
|
|
|
+ *
|
|
|
* @param sheetName 工作表的名称
|
|
|
* @return 结果
|
|
|
*/
|
|
@@ -343,7 +534,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 对list数据源将其里面的数据导入到excel表单
|
|
|
- *
|
|
|
+ *
|
|
|
* @return 结果
|
|
|
*/
|
|
|
public void exportExcel(OutputStream outputStream)
|
|
@@ -379,34 +570,13 @@ 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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 填充excel数据
|
|
|
- *
|
|
|
+ *
|
|
|
* @param index 序号
|
|
|
* @param row 单元格行
|
|
|
*/
|
|
@@ -431,9 +601,43 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 填充excel数据
|
|
|
+ *
|
|
|
+ * @param index 序号
|
|
|
+ * @param row 单元格行
|
|
|
+ */
|
|
|
+ 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);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 创建表格样式
|
|
|
- *
|
|
|
+ *
|
|
|
* @param wb 工作薄对象
|
|
|
* @return 样式列表
|
|
|
*/
|
|
@@ -471,7 +675,7 @@ public class ExcelUtil<T>
|
|
|
headerFont.setColor(IndexedColors.WHITE.getIndex());
|
|
|
style.setFont(headerFont);
|
|
|
styles.put("header", style);
|
|
|
-
|
|
|
+
|
|
|
style = wb.createCellStyle();
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
@@ -513,9 +717,62 @@ public class ExcelUtil<T>
|
|
|
return cell;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建单元格
|
|
|
+ */
|
|
|
+ public void createCellByColumn(GenTableColumn column, Row row, int columnIndex) {
|
|
|
+ // 创建列
|
|
|
+ Cell cell = row.createCell(columnIndex);
|
|
|
+ // 写入列信息
|
|
|
+ cell.setCellValue(column.getColumnComment());
|
|
|
+ setDataValidationByColumn(column, row, columnIndex);
|
|
|
+ if (GenTableColumn.IS_REQUIRED.equals(column.getIsRequired())) {
|
|
|
+ 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) 10);
|
|
|
+// headerFont.setBold(true);
|
|
|
+ headerFont.setColor(IndexedColors.RED.getIndex());
|
|
|
+ style.setFont(headerFont);
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建单元格
|
|
|
+ */
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设置单元格信息
|
|
|
- *
|
|
|
+ *
|
|
|
* @param value 单元格值
|
|
|
* @param attr 注解相关
|
|
|
* @param cell 单元格信息
|
|
@@ -543,7 +800,7 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取画布
|
|
|
*/
|
|
@@ -601,6 +858,132 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 创建表格样式
|
|
|
+ */
|
|
|
+ public void setDataValidationByColumn(GenTableColumn column, Row row, int columnIndex) {
|
|
|
+ if (column.getColumnComment().contains("注:")) {
|
|
|
+ sheet.setColumnWidth(columnIndex, 6000);
|
|
|
+ } else {
|
|
|
+ // 设置列宽
|
|
|
+ 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列提示.
|
|
|
+// setXSSFPrompt(sheet, "", attr.prompt(), 1, 100, columnIndex, columnIndex);
|
|
|
+// }
|
|
|
+ // 如果设置了combo属性则本列只能选择不能输入
|
|
|
+// if (attr.combo().length > 0) {
|
|
|
+// // 这里默认设了2-101列只能选择不能输入.
|
|
|
+// setXSSFValidation(sheet, attr.combo(), 1, 100, columnIndex, columnIndex);
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加单元格
|
|
|
+ */
|
|
|
+ public void addCellByColumns(Row row, Map<String, Object> jsonObject, List<GenTableColumn> listVisibleColumns) {
|
|
|
+ Cell cell;
|
|
|
+ try {
|
|
|
+ int columnIndex = 0;
|
|
|
+ for (GenTableColumn column : listVisibleColumns) {
|
|
|
+ for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
|
|
|
+ String columnName = entry.getKey();
|
|
|
+ Object value = entry.getValue();
|
|
|
+ if (!column.getColumnName().equals(columnName)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置行高
|
|
|
+ row.setHeight((short) 600);
|
|
|
+ // 创建cell
|
|
|
+ cell = row.createCell(columnIndex++);
|
|
|
+ cell.setCellStyle(styles.get("data"));
|
|
|
+
|
|
|
+ if (isNotEmpty(value) && FormDataConstant.BYTE_ARRAY.equals(value.getClass().getSimpleName())) {
|
|
|
+ // blob
|
|
|
+ cell.setCellValue(byteToString(((byte[]) value)));
|
|
|
+ } else if (isNotEmpty(column.getDictType())) {
|
|
|
+ // 字典
|
|
|
+ cell.setCellValue(handleDictForExport(jsonObject, column));
|
|
|
+ } else if (NEED_CONVERT_DATE_LIST.contains(column.getColumnType())) {
|
|
|
+ // time
|
|
|
+ if (isNotEmpty(value)) {
|
|
|
+ cell.setCellValue(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, TypeUtils.castToTimestamp(value)));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 设置列类型
|
|
|
+ cell.setCellValue(String.valueOf(value));
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 用于读取对象中的属性
|
|
|
+// Object value = getTargetValueByJSONObject(vo, field, attr);
|
|
|
+// String dateFormat = attr.dateFormat();
|
|
|
+// String readConverterExp = attr.readConverterExp();
|
|
|
+// String separator = attr.separator();
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+//
|
|
|
+// if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) {
|
|
|
+// cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
|
|
|
+// } else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) {
|
|
|
+// cell.setCellValue(convertByExp(Convert.toStr(value), readConverterExp, separator));
|
|
|
+// } else if (value instanceof BigDecimal && -1 != attr.scale()) {
|
|
|
+// cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).toString());
|
|
|
+// } else {
|
|
|
+// // 设置列类型
|
|
|
+// setCellVo(value, attr, cell);
|
|
|
+// }
|
|
|
+//
|
|
|
+// addStatisticsData(columnIndex, Convert.toStr(value), attr);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导出Excel失败: {}", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加单元格
|
|
|
+ */
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 添加单元格
|
|
|
*/
|
|
@@ -653,7 +1036,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 设置 POI XSSFSheet 单元格提示
|
|
|
- *
|
|
|
+ *
|
|
|
* @param sheet 表单
|
|
|
* @param promptTitle 提示标题
|
|
|
* @param promptContent 提示内容
|
|
@@ -676,7 +1059,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 设置某些列的值只能输入预制的数据,显示下拉框.
|
|
|
- *
|
|
|
+ *
|
|
|
* @param sheet 要设置的sheet.
|
|
|
* @param textlist 下拉框显示的内容
|
|
|
* @param firstRow 开始行
|
|
@@ -710,7 +1093,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 解析导出值 0=男,1=女,2=未知
|
|
|
- *
|
|
|
+ *
|
|
|
* @param propertyValue 参数值
|
|
|
* @param converterExp 翻译注解
|
|
|
* @param separator 分隔符
|
|
@@ -747,7 +1130,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 反向解析值 男=0,女=1,未知=2
|
|
|
- *
|
|
|
+ *
|
|
|
* @param propertyValue 参数值
|
|
|
* @param converterExp 翻译注解
|
|
|
* @param separator 分隔符
|
|
@@ -831,7 +1214,32 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 获取bean中的属性值
|
|
|
- *
|
|
|
+ *
|
|
|
+ * @param vo 实体对象
|
|
|
+ * @param field 字段
|
|
|
+ * @param excel 注解
|
|
|
+ * @return 最终的属性值
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private Object getTargetValueByJSONObject(T vo, Field field, Excel excel) throws Exception {
|
|
|
+ Object o = field.get(vo);
|
|
|
+ if (StringUtils.isNotEmpty(excel.targetAttr())) {
|
|
|
+ String target = excel.targetAttr();
|
|
|
+ if (target.contains(".")) {
|
|
|
+ String[] targets = target.split("[.]");
|
|
|
+ for (String name : targets) {
|
|
|
+ o = getValue(o, name);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ o = getValue(o, target);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return o;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取bean中的属性值
|
|
|
+ *
|
|
|
* @param vo 实体对象
|
|
|
* @param field 字段
|
|
|
* @param excel 注解
|
|
@@ -862,7 +1270,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 以类的属性的get方法方法形式获取值
|
|
|
- *
|
|
|
+ *
|
|
|
* @param o
|
|
|
* @param name
|
|
|
* @return value
|
|
@@ -911,7 +1319,7 @@ public class ExcelUtil<T>
|
|
|
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
|
|
this.maxHeight = getRowHeight();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 根据注解获取最大行高
|
|
|
*/
|
|
@@ -947,7 +1355,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 创建工作表
|
|
|
- *
|
|
|
+ *
|
|
|
* @param sheetNo sheet数量
|
|
|
* @param index 序号
|
|
|
*/
|
|
@@ -968,7 +1376,7 @@ public class ExcelUtil<T>
|
|
|
|
|
|
/**
|
|
|
* 获取单元格值
|
|
|
- *
|
|
|
+ *
|
|
|
* @param row 获取的行
|
|
|
* @param column 获取单元格列号
|
|
|
* @return 单元格值
|
|
@@ -1025,4 +1433,102 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
return val;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 把新增可见或者修改可见或者.....的列过滤出来
|
|
|
+ *
|
|
|
+ * @param allColumns 所有的列
|
|
|
+ * @param maskIndex 那六个1的顺序 [1,1,1,1,1,1]
|
|
|
+ * sort=0 => 新增可见
|
|
|
+ * sort=1 => 新增可修改
|
|
|
+ * sort=2 => 修改可见
|
|
|
+ * sort=3 => 修改可修改
|
|
|
+ * sort=4 => 列表可见
|
|
|
+ * sort=5 => 列表可修改
|
|
|
+ * @return List<GenTableColumn>
|
|
|
+ */
|
|
|
+ public static List<GenTableColumn> filterData(List<GenTableColumn> allColumns, int maskIndex, Predicate<String> predicate) {
|
|
|
+ assert maskIndex < 6;
|
|
|
+ List<GenTableColumn> returnData = new ArrayList<>(16);
|
|
|
+ for (GenTableColumn allColumn : allColumns) {
|
|
|
+ if (HR.equals(allColumn.getHtmlType())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (checkMask(maskIndex, allColumn, predicate)) {
|
|
|
+ returnData.add(allColumn);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return returnData;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 判断mask == 0 返回true
|
|
|
+ *
|
|
|
+ * @param maskIndex mask的索引
|
|
|
+ * @param column column
|
|
|
+ * @param predicate predicate 注意传的是1还是0
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public static boolean checkMask(int maskIndex, GenTableColumn column, Predicate<String> predicate) {
|
|
|
+ String mask = column.getMask();
|
|
|
+ String columnName = column.getColumnName();
|
|
|
+ String tableName = column.getTableName();
|
|
|
+ String errorMsg = String.format("mask is empty, tableName = [ %s ], columnName = [ %s ]", tableName, columnName);
|
|
|
+ String[] maskArray = requireNonNull(mask, errorMsg).split("");
|
|
|
+ assert maskArray.length == 6;
|
|
|
+ String maskMark = maskArray[maskIndex];
|
|
|
+ return predicate.test(maskMark);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String byteToString(byte[] bytes) {
|
|
|
+ String result = "";
|
|
|
+ if (ArrayUtils.isNotEmpty(bytes)) {
|
|
|
+ result = new String(bytes, StandardCharsets.UTF_8);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 功能描述: 处理字典值,不查询所有的字典值, 不放入sysDictData,只根据dictValue查询dictLabel
|
|
|
+ *
|
|
|
+ * @param jsonObject db中查出的结果
|
|
|
+ * @param column 所有的列
|
|
|
+ */
|
|
|
+ public static String handleDictForExport(Map<String, Object> jsonObject, GenTableColumn column) {
|
|
|
+ if (isEmpty(jsonObject)) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ String columnName = column.getColumnName();
|
|
|
+ // 列表需要字典值的dictLabel, 不放入sysDictData
|
|
|
+ List<SysDictData> dictDataList = column.getSysDictData();
|
|
|
+ SysDictData sysDictData = filterOne(dictDataList, dict -> dict.getDictValue().equals(jsonObject.get(columnName)));
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|