Search in sources :

Example 1 with ExcelFormat

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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) FileOutputStream(java.io.FileOutputStream) InputStreamResource(org.springframework.core.io.InputStreamResource) Resource(org.springframework.core.io.Resource) ExcelFormat(de.metas.impexp.excel.ExcelFormat) File(java.io.File) FileInputStream(java.io.FileInputStream) InputStreamResource(org.springframework.core.io.InputStreamResource) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

ExcelFormat (de.metas.impexp.excel.ExcelFormat)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamResource (org.springframework.core.io.InputStreamResource)1 Resource (org.springframework.core.io.Resource)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1