use of de.metas.impexp.excel.ExcelFormat in project metasfresh-webui-api by metasfresh.
the class ViewRestController method exportToExcel.
@GetMapping("/{viewId}/export/excel")
public ResponseEntity<Resource> exportToExcel(@PathVariable("windowId") final String windowIdStr, @PathVariable(PARAM_ViewId) final String viewIdStr, @RequestParam(name = "selectedIds", required = false) @ApiParam("comma separated IDs") final String selectedIdsListStr) throws Exception {
userSession.assertLoggedIn();
final ViewId viewId = ViewId.ofViewIdString(viewIdStr, WindowId.fromJson(windowIdStr));
final ExcelFormat excelFormat = ExcelFormats.getDefaultFormat();
final File tmpFile = File.createTempFile("exportToExcel", "." + excelFormat.getFileExtension());
try (final FileOutputStream out = new FileOutputStream(tmpFile)) {
ViewExcelExporter.builder().excelFormat(excelFormat).view(viewsRepo.getView(viewId)).rowIds(DocumentIdsSelection.ofCommaSeparatedString(selectedIdsListStr)).layout(viewsRepo.getViewLayout(viewId.getWindowId(), JSONViewDataType.grid, ViewProfileId.NULL)).language(userSession.getLanguage()).zoneId(userSession.getTimeZone()).build().export(out);
}
// TODO: use a better name
final String filename = "report." + excelFormat.getFileExtension();
final String contentType = MimeType.getMimeType(filename);
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType(contentType));
headers.set(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + filename + "\"");
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
final ResponseEntity<Resource> response = new ResponseEntity<>(new InputStreamResource(new FileInputStream(tmpFile)), headers, HttpStatus.OK);
return response;
}
Aggregations