У меня уже есть метод, который загружает файл Excel. Тем не менее, я хочу одновременно загрузить файл Excel и получить информацию (которая является объектом) об этом файле Excel.
Я попробовал следующий код, однако он возвращает только файл Excel.
@GetMapping(value = "/export")
public ImportResult exportAlarms(HttpServletRequest request,
HttpServletResponse response) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (Workbook workbook = new XSSFWorkbook()) {
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.BLUE.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
ImportResult result = alarmExporter.exportAlarms(workbook, headerCellStyle, null);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
response.addHeader("Content-Disposition", "attachment; filename=\"alarms.xlsx\"");
workbook.write(response.getOutputStream());
response.getOutputStream().flush();
return result;
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
Это код ImportResult:
public class ImportResult {
private final int successCount;
private final int failCount;
private final List<String> errors;
public ImportResult(int successCount, int failCount, List<String> errors) {
this.successCount = successCount;
this.failCount = failCount;
this.errors = errors == null ? new ArrayList<>() : new ArrayList<>(errors);
}
public int getSuccessCount() {
return successCount;
}
public int getFailCount() {
return failCount;
}
public List<String> getErrors() {
return Collections.unmodifiableList(errors);
}
public boolean hasErrors() {
return !errors.isEmpty();
}
public ImportResult combine(ImportResult another) {
int successCount = another.getSuccessCount() + this.getSuccessCount();
int failCount = another.getFailCount() + this.getFailCount();
List<String> errors = new ArrayList<>(another.getErrors());
errors.addAll(this.getErrors());
return new ImportResult(successCount, failCount, errors);
}