From 05d1834ab81819e750899b93c0a02dbbf4d48c71 Mon Sep 17 00:00:00 2001 From: lbw Date: Thu, 29 Jun 2023 07:28:04 +0800 Subject: [PATCH] =?UTF-8?q?:green=5Fheart:=20=E4=BF=AE=E5=A4=8D=20CI=20?= =?UTF-8?q?=E6=9E=84=E5=BB=BA,=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E8=AF=9D=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/ResponseExcelAutoConfiguration.java | 2 +- .../aop/RequestExcelArgumentResolver.java | 10 +++++-- .../aop/ResponseExcelReturnValueHandler.java | 6 ++-- .../converters/LocalDateStringConverter.java | 2 +- .../LocalDateTimeStringConverter.java | 30 +++++++++---------- .../handler/AbstractSheetWriteHandler.java | 17 ++++++----- .../handler/DefaultAnalysisEventListener.java | 5 ++-- .../head/I18nHeaderCellWriteHandler.java | 4 +-- .../plugin/excel/read/ReaderExcelTests.java | 8 +++-- 9 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/pig4cloud/plugin/excel/ResponseExcelAutoConfiguration.java b/src/main/java/com/pig4cloud/plugin/excel/ResponseExcelAutoConfiguration.java index d6daa6a..d3a977b 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/ResponseExcelAutoConfiguration.java +++ b/src/main/java/com/pig4cloud/plugin/excel/ResponseExcelAutoConfiguration.java @@ -63,7 +63,7 @@ public DynamicNameAspect dynamicNameAspect(NameProcessor nameProcessor) { @PostConstruct public void setReturnValueHandlers() { List returnValueHandlers = requestMappingHandlerAdapter - .getReturnValueHandlers(); + .getReturnValueHandlers(); List newHandlers = new ArrayList<>(); newHandlers.add(responseExcelReturnValueHandler); diff --git a/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java b/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java index 3033583..4693d45 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java +++ b/src/main/java/com/pig4cloud/plugin/excel/aop/RequestExcelArgumentResolver.java @@ -71,9 +71,13 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m Class excelModelClass = ResolvableType.forMethodParameter(parameter).getGeneric(0).resolve(); // 这里需要指定读用哪个 class 去读,然后读取第一个 sheet 文件流会自动关闭 - EasyExcel.read(inputStream, excelModelClass, readListener).registerConverter(LocalDateStringConverter.INSTANCE) - .registerConverter(LocalDateTimeStringConverter.INSTANCE).ignoreEmptyRow(requestExcel.ignoreEmptyRow()) - .sheet().headRowNumber(requestExcel.headRowNumber()).doRead(); + EasyExcel.read(inputStream, excelModelClass, readListener) + .registerConverter(LocalDateStringConverter.INSTANCE) + .registerConverter(LocalDateTimeStringConverter.INSTANCE) + .ignoreEmptyRow(requestExcel.ignoreEmptyRow()) + .sheet() + .headRowNumber(requestExcel.headRowNumber()) + .doRead(); // 校验失败的数据处理 交给 BindResult WebDataBinder dataBinder = webDataBinderFactory.createBinder(webRequest, readListener.getErrors(), "excel"); diff --git a/src/main/java/com/pig4cloud/plugin/excel/aop/ResponseExcelReturnValueHandler.java b/src/main/java/com/pig4cloud/plugin/excel/aop/ResponseExcelReturnValueHandler.java index 87893d1..8d87390 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/aop/ResponseExcelReturnValueHandler.java +++ b/src/main/java/com/pig4cloud/plugin/excel/aop/ResponseExcelReturnValueHandler.java @@ -51,8 +51,10 @@ public void handleReturnValue(Object o, MethodParameter parameter, ModelAndViewC Assert.state(responseExcel != null, "No @ResponseExcel"); mavContainer.setRequestHandled(true); - sheetWriteHandlerList.stream().filter(handler -> handler.support(o)).findFirst() - .ifPresent(handler -> handler.export(o, response, responseExcel)); + sheetWriteHandlerList.stream() + .filter(handler -> handler.support(o)) + .findFirst() + .ifPresent(handler -> handler.export(o, response, responseExcel)); } } diff --git a/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateStringConverter.java b/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateStringConverter.java index 377e0ca..a473003 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateStringConverter.java +++ b/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateStringConverter.java @@ -41,7 +41,7 @@ public LocalDate convertToJavaData(ReadCellData cellData, ExcelContentProperty c } else { DateTimeFormatter formatter = DateTimeFormatter - .ofPattern(contentProperty.getDateTimeFormatProperty().getFormat()); + .ofPattern(contentProperty.getDateTimeFormatProperty().getFormat()); return LocalDate.parse(cellData.getStringValue(), formatter); } } diff --git a/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateTimeStringConverter.java b/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateTimeStringConverter.java index 9487bd6..def0911 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateTimeStringConverter.java +++ b/src/main/java/com/pig4cloud/plugin/excel/converters/LocalDateTimeStringConverter.java @@ -73,21 +73,21 @@ public WriteCellData convertToExcelData(LocalDateTime value, ExcelConten private static String switchDateFormat(String dateString) { int length = dateString.length(); switch (length) { - case 19: - if (dateString.contains(MINUS)) { - return DateUtils.DATE_FORMAT_19; - } - else { - return DateUtils.DATE_FORMAT_19_FORWARD_SLASH; - } - case 17: - return DateUtils.DATE_FORMAT_17; - case 14: - return DateUtils.DATE_FORMAT_14; - case 10: - return DateUtils.DATE_FORMAT_10; - default: - throw new IllegalArgumentException("can not find date format for:" + dateString); + case 19: + if (dateString.contains(MINUS)) { + return DateUtils.DATE_FORMAT_19; + } + else { + return DateUtils.DATE_FORMAT_19_FORWARD_SLASH; + } + case 17: + return DateUtils.DATE_FORMAT_17; + case 14: + return DateUtils.DATE_FORMAT_14; + case 10: + return DateUtils.DATE_FORMAT_10; + default: + throw new IllegalArgumentException("can not find date format for:" + dateString); } } diff --git a/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java b/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java index 093d060..151d4ab 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java +++ b/src/main/java/com/pig4cloud/plugin/excel/handler/AbstractSheetWriteHandler.java @@ -82,15 +82,16 @@ public void check(ResponseExcel responseExcel) { public void export(Object o, HttpServletResponse response, ResponseExcel responseExcel) { check(responseExcel); RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); - String name = (String) Objects.requireNonNull(requestAttributes).getAttribute(DynamicNameAspect.EXCEL_NAME_KEY, - RequestAttributes.SCOPE_REQUEST); + String name = (String) Objects.requireNonNull(requestAttributes) + .getAttribute(DynamicNameAspect.EXCEL_NAME_KEY, RequestAttributes.SCOPE_REQUEST); if (name == null) { name = UUID.randomUUID().toString(); } String fileName = String.format("%s%s", URLEncoder.encode(name, "UTF-8"), responseExcel.suffix().getValue()); // 根据实际的文件类型找到对应的 contentType - String contentType = MediaTypeFactory.getMediaType(fileName).map(MediaType::toString) - .orElse("application/vnd.ms-excel"); + String contentType = MediaTypeFactory.getMediaType(fileName) + .map(MediaType::toString) + .orElse("application/vnd.ms-excel"); response.setContentType(contentType); response.setCharacterEncoding("utf-8"); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename*=utf-8''" + fileName); @@ -106,9 +107,11 @@ public void export(Object o, HttpServletResponse response, ResponseExcel respons @SneakyThrows(IOException.class) public ExcelWriter getExcelWriter(HttpServletResponse response, ResponseExcel responseExcel) { ExcelWriterBuilder writerBuilder = EasyExcel.write(response.getOutputStream()) - .registerConverter(LocalDateStringConverter.INSTANCE) - .registerConverter(LocalDateTimeStringConverter.INSTANCE).autoCloseStream(true) - .excelType(responseExcel.suffix()).inMemory(responseExcel.inMemory()); + .registerConverter(LocalDateStringConverter.INSTANCE) + .registerConverter(LocalDateTimeStringConverter.INSTANCE) + .autoCloseStream(true) + .excelType(responseExcel.suffix()) + .inMemory(responseExcel.inMemory()); if (StringUtils.hasText(responseExcel.password())) { writerBuilder.password(responseExcel.password()); diff --git a/src/main/java/com/pig4cloud/plugin/excel/handler/DefaultAnalysisEventListener.java b/src/main/java/com/pig4cloud/plugin/excel/handler/DefaultAnalysisEventListener.java index c672867..67b0147 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/handler/DefaultAnalysisEventListener.java +++ b/src/main/java/com/pig4cloud/plugin/excel/handler/DefaultAnalysisEventListener.java @@ -35,8 +35,9 @@ public void invoke(Object o, AnalysisContext analysisContext) { Set> violations = Validators.validate(o); if (!violations.isEmpty()) { - Set messageSet = violations.stream().map(ConstraintViolation::getMessage) - .collect(Collectors.toSet()); + Set messageSet = violations.stream() + .map(ConstraintViolation::getMessage) + .collect(Collectors.toSet()); errorMessageList.add(new ErrorMessage(lineNum, messageSet)); } else { diff --git a/src/main/java/com/pig4cloud/plugin/excel/head/I18nHeaderCellWriteHandler.java b/src/main/java/com/pig4cloud/plugin/excel/head/I18nHeaderCellWriteHandler.java index 011f61f..894fde0 100644 --- a/src/main/java/com/pig4cloud/plugin/excel/head/I18nHeaderCellWriteHandler.java +++ b/src/main/java/com/pig4cloud/plugin/excel/head/I18nHeaderCellWriteHandler.java @@ -51,8 +51,8 @@ public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder if (CollectionUtils.isNotEmpty(originHeadNameList)) { // 国际化处理 List i18nHeadNames = originHeadNameList.stream() - .map(headName -> propertyPlaceholderHelper.replacePlaceholders(headName, placeholderResolver)) - .collect(Collectors.toList()); + .map(headName -> propertyPlaceholderHelper.replacePlaceholders(headName, placeholderResolver)) + .collect(Collectors.toList()); head.setHeadNameList(i18nHeadNames); } } diff --git a/src/test/java/com/pig4cloud/plugin/excel/read/ReaderExcelTests.java b/src/test/java/com/pig4cloud/plugin/excel/read/ReaderExcelTests.java index 9fa023f..3dfc97f 100644 --- a/src/test/java/com/pig4cloud/plugin/excel/read/ReaderExcelTests.java +++ b/src/test/java/com/pig4cloud/plugin/excel/read/ReaderExcelTests.java @@ -27,15 +27,17 @@ class ReaderExcelTests { @Test void indexOrNameRead() throws IOException { ClassPathResource classPathResource = new ClassPathResource("tmp/indexOrName.xlsx"); - EasyExcel.read(classPathResource.getInputStream(), IndexOrNameData.class, new IndexOrNameDataListener()).sheet() - .doRead(); + EasyExcel.read(classPathResource.getInputStream(), IndexOrNameData.class, new IndexOrNameDataListener()) + .sheet() + .doRead(); } @Test void indexOrNameRead1() throws IOException { ClassPathResource classPathResource = new ClassPathResource("tmp/indexOrName1.xlsx"); EasyExcel.read(classPathResource.getInputStream(), IndexOrNameData1.class, new IndexOrNameDataListener1()) - .sheet().doRead(); + .sheet() + .doRead(); } }