Skip to content

Commit

Permalink
💚 修复 CI 构建,代码格式化话处理
Browse files Browse the repository at this point in the history
  • Loading branch information
lbw committed Jun 28, 2023
1 parent d2f5bfc commit 05d1834
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public DynamicNameAspect dynamicNameAspect(NameProcessor nameProcessor) {
@PostConstruct
public void setReturnValueHandlers() {
List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter
.getReturnValueHandlers();
.getReturnValueHandlers();

List<HandlerMethodReturnValueHandler> newHandlers = new ArrayList<>();
newHandlers.add(responseExcelReturnValueHandler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,21 @@ public WriteCellData<String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ public void invoke(Object o, AnalysisContext analysisContext) {

Set<ConstraintViolation<Object>> violations = Validators.validate(o);
if (!violations.isEmpty()) {
Set<String> messageSet = violations.stream().map(ConstraintViolation::getMessage)
.collect(Collectors.toSet());
Set<String> messageSet = violations.stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.toSet());
errorMessageList.add(new ErrorMessage(lineNum, messageSet));
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder
if (CollectionUtils.isNotEmpty(originHeadNameList)) {
// 国际化处理
List<String> i18nHeadNames = originHeadNameList.stream()
.map(headName -> propertyPlaceholderHelper.replacePlaceholders(headName, placeholderResolver))
.collect(Collectors.toList());
.map(headName -> propertyPlaceholderHelper.replacePlaceholders(headName, placeholderResolver))
.collect(Collectors.toList());
head.setHeadNameList(i18nHeadNames);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

0 comments on commit 05d1834

Please sign in to comment.