| | |
| | | |
| | | import cn.afterturn.easypoi.excel.annotation.Excel; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult; |
| | | import cn.afterturn.easypoi.handler.inter.IExcelDataHandler; |
| | | import com.google.common.collect.Lists; |
| | | import com.vincent.rsf.common.domain.BeanValidators; |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.vincent.rsf.framework.common.Cools; |
| | | import com.vincent.rsf.server.common.handler.AggregationDataHandler; |
| | | import com.vincent.rsf.server.common.handler.ExcelDictHandlerImpl; |
| | | import com.vincent.rsf.framework.common.SpringUtils; |
| | | import com.vincent.rsf.framework.exception.CoolException; |
| | | import com.vincent.rsf.server.manager.entity.Matnr; |
| | | import com.vincent.rsf.server.manager.entity.excel.MatnrsTemplate; |
| | | import com.vincent.rsf.server.manager.entity.excel.annotation.ExcelComment; |
| | | import com.vincent.rsf.server.system.entity.Fields; |
| | | import com.vincent.rsf.server.system.service.FieldsService; |
| | | import io.swagger.annotations.ApiModelProperty; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
| | | import org.apache.poi.ss.formula.functions.T; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.validation.ConstraintViolationException; |
| | | import javax.validation.Validation; |
| | | import javax.validation.Validator; |
| | | import javax.validation.ValidatorFactory; |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Modifier; |
| | |
| | | try { |
| | | response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("export", "UTF-8")); |
| | | workbook.write(response.getOutputStream()); |
| | | } catch (IOException ignore) {} |
| | | } catch (IOException ignore) { |
| | | } |
| | | } |
| | | |
| | | public static <T> Workbook create(List<T> list, Class<T> clz) { |
| | | return create(list, clz, false); |
| | | return create(list, clz, false); |
| | | } |
| | | |
| | | public static <T> Workbook create(List<T> list, Class<T> clz, boolean flagTemplate) { |
| | |
| | | Row header = sheet.createRow(0); |
| | | Field[] fields = Cools.getAllFields(clz); |
| | | int headerIdx = 0; |
| | | FieldsService itemService = SpringUtils.getBean(FieldsService.class); |
| | | List<Fields> sysFields = itemService.list(new LambdaQueryWrapper<Fields>() |
| | | .eq(Fields::getStatus, 1) |
| | | .eq(Fields::getFlagEnable, 1)); |
| | | |
| | | |
| | | for (Field field : fields) { |
| | | if (Modifier.isFinal(field.getModifiers()) |
| | | || Modifier.isStatic(field.getModifiers()) |
| | |
| | | } |
| | | String memo = "Undefined"; |
| | | if (flagTemplate) { |
| | | if (field.isAnnotationPresent(ExcelComment.class)) { |
| | | memo = field.getAnnotation(ExcelComment.class).value(); |
| | | } |
| | | memo = field.getName(); |
| | | } else { |
| | | if (field.isAnnotationPresent(Excel.class)) { |
| | | memo = field.getAnnotation(Excel.class).name(); |
| | |
| | | header.createCell(headerIdx).setCellValue(memo); |
| | | headerIdx++; |
| | | } |
| | | |
| | | int rowIndex = 1; |
| | | for (T t : list) { |
| | | Row row = sheet.createRow(rowIndex++); |
| | | |
| | | int cellIndex = 0; |
| | | for (Field field : fields) { |
| | | if (Modifier.isFinal(field.getModifiers()) |
| | | || Modifier.isStatic(field.getModifiers()) |
| | | || Modifier.isTransient(field.getModifiers())) { |
| | | continue; |
| | | } |
| | | |
| | | // 此行很重要,特别是字段为private时 |
| | | field.setAccessible(true); |
| | | Object value = null; |
| | | try { |
| | | value = field.get(t); |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (value != null) { |
| | | if (value instanceof Date) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | row.createCell(cellIndex).setCellValue(sdf.format((Date) value)); |
| | | if (clz.equals(MatnrsTemplate.class)) { |
| | | //添加扩展字段别名 |
| | | if (!sysFields.isEmpty()) { |
| | | for (Fields field : sysFields) { |
| | | if (flagTemplate) { |
| | | header.createCell(headerIdx).setCellValue(field.getFields()); |
| | | } else { |
| | | row.createCell(cellIndex).setCellValue(value.toString()); |
| | | header.createCell(headerIdx).setCellValue(field.getFieldsAlise()); |
| | | } |
| | | headerIdx++; |
| | | } |
| | | cellIndex++; |
| | | } |
| | | } |
| | | |
| | | int rowIndex = 1; |
| | | if (!Objects.isNull(list)) { |
| | | for (T t : list) { |
| | | Row row = sheet.createRow(rowIndex++); |
| | | |
| | | int cellIndex = 0; |
| | | for (Field field : fields) { |
| | | if (Modifier.isFinal(field.getModifiers()) |
| | | || Modifier.isStatic(field.getModifiers()) |
| | | || Modifier.isTransient(field.getModifiers())) { |
| | | continue; |
| | | } |
| | | |
| | | // 此行很重要,特别是字段为private时 |
| | | field.setAccessible(true); |
| | | Object value = null; |
| | | try { |
| | | value = field.get(t); |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (value != null) { |
| | | if (value instanceof Date) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | row.createCell(cellIndex).setCellValue(sdf.format((Date) value)); |
| | | } else { |
| | | row.createCell(cellIndex).setCellValue(value.toString()); |
| | | } |
| | | } |
| | | cellIndex++; |
| | | } |
| | | } |
| | | } |
| | | for (int i = 0; i <= fields.length; i++) { |
| | | sheet.autoSizeColumn(i); |
| | | } |
| | |
| | | /** |
| | | * 添加导入excel配置参数 |
| | | * 注:默认配置可满足当前需求 |
| | | * |
| | | * @return |
| | | */ |
| | | public static ImportParams getDefaultImportParams() { |
| | |
| | | } |
| | | |
| | | /** |
| | | * @author Ryan |
| | | * @description Excel导出Map格式表格 |
| | | * @param |
| | | * @return |
| | | * @time 2025/3/18 09:30 |
| | | */ |
| | | public static void exportForMap(HttpServletResponse response, Class cls, String fileName, List<List<Object>> data) { |
| | | try { |
| | | response.setContentType("application/octet-stream; charset=utf-8"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | String name = URLEncoder.encode(fileName, "UTF-8"); |
| | | response.setHeader("Content-disposition", "attachment;filename=" + name + ".xlsx"); |
| | | EasyExcel.write(response.getOutputStream()).head(getHeader(cls)).sheet().doWrite(null); |
| | | } catch (IOException exception) { |
| | | exception.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @author Ryan |
| | | * @description excel导出map模式 |
| | | * @param Class<T> 模板类型 |
| | | * @return List<List<String>> 表头信息 |
| | | * @time 2025/3/18 09:26 |
| | | */ |
| | | public static List<List<String>> getHeader(Class<T> t) { |
| | | List<List<String>> headList = new ArrayList<List<String>>(); |
| | | if (t.getSuperclass().isInstance(MatnrsTemplate.class)) { |
| | | Field[] allFields = Cools.getAllFields(t); |
| | | if (Objects.isNull(allFields) || allFields.length < 1) { |
| | | throw new CoolException("模板列不能为空!!"); |
| | | } |
| | | for (Field field : allFields) { |
| | | List<String> list = new ArrayList<String>(); |
| | | String fieldName = ""; |
| | | if (field.isAnnotationPresent(Excel.class)){ |
| | | fieldName = field.getAnnotation(Excel.class).name(); |
| | | } |
| | | |
| | | list.add(field.getName()); |
| | | headList.add(list); |
| | | } |
| | | |
| | | FieldsService itemService = SpringUtils.getBean(FieldsService.class); |
| | | List<Fields> sysFields = itemService.list(new LambdaQueryWrapper<Fields>() |
| | | .eq(Fields::getStatus, 1) |
| | | .eq(Fields::getFlagEnable, 1)); |
| | | //添加扩展字段别名 |
| | | if (!sysFields.isEmpty()) { |
| | | sysFields.forEach(fields1 -> { |
| | | List<String> list = new ArrayList<String>(); |
| | | list.add(fields1.getFieldsAlise()); |
| | | headList.add(list); |
| | | }); |
| | | } |
| | | } |
| | | return headList; |
| | | } |
| | | |
| | | /** |
| | | * 根据 {@code tClass} 相关成员变量的 {@link ExcelComment#example()} 字段创建模拟数据,暂不支持 复杂类型 |
| | | * |
| | | * @param tClass |
| | | * @return |
| | | */ |