4

The REST method to return outputStream data to download an Excel spreadsheet:

@RequestMapping(value = "/downloadxls", method = RequestMethod.GET)
public @ResponseBody void getRecordsAndExportExcel(@RequestParam("search_param") String students, HttpServletResponse response) {
    response.setContentType("application/vnd.ms-excel");

 Workbook hssfWorkbook = exportExcelService.getExcelStudents(students);
        try {
            OutputStream out = response.getOutputStream();
            hssfWorkbook.write(out);
            out.flush();
            out.close();

        } catch (IOException e) {
           logger.error("Error exporting to excel:" + e); 
        }
}

I am getting the data as bytes, but in Angular I am trying to present it as an Excel spreadsheet; but it won't download. I am doing this for the conversion:

var blob = new Blob([result.data],  {type : 'application/vnd.openxmlformats-officedocument.presentationml.presentation;charset=UTF-8'});
         FileSaver.saveAs(blob, "MyData.xls");

The request and response headers:

Access-Control-Allow-Headers:x-requested-with, content-type
Access-Control-Allow-Methods:GET OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:3600
Content-Type:application/vnd.ms-excel;charset=UTF-8
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
X-Application-Context:application:8080

Request Headers

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,ms;q=0.6
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:9000
Referer:http://localhost:9000/

I am making a GET-call from frontend using Angular and calling backend to download the data as an Excel spreadsheet, but it is not able to convert the output stream to blob/excel. How can I present an Excel spreadsheet as download?

Rob
  • 14,746
  • 28
  • 47
  • 65
user3428736
  • 864
  • 2
  • 13
  • 33
  • Can you elaborate on *"it won't download"*? Do you get any errors in the console? What happens after the `window.open()` call? – Phil Sep 26 '16 at 01:09
  • Possible duplicate of [JavaScript blob filename without link](http://stackoverflow.com/questions/19327749/javascript-blob-filename-without-link) – Phil Sep 26 '16 at 01:12
  • I am not getting any window.open in html.I am receiving the bytes from backend to ui (also if i try to write excel in backend it is fine) and in UI I have a model window to click download option() and I am calling my JS to download. I am not getting any error in console but if try to display data it is displaying as bytes in console. Please suggest Is there any change I need to add in HTML and JS to get the files. – user3428736 Sep 26 '16 at 01:17
  • I have tried using FileSaver it is downloading but the bytes are writing to the excel, Please suggest why the data is not converted from bytes. – user3428736 Sep 26 '16 at 01:53

1 Answers1

0

The way I would present an excel sheet in my application is to return the path+filename of the excel sheet to show and then open the link using angular in a new tab.

In the service class

ExportExcel response = new ExportExcel();
response.setPath(excelPath);
return response;

Controller class

Response response = excelService.generateExcel();
return new ResponseEntity<>(response, HttpStatus.OK);

In your angular controller:

if(response.status === 200){
var excelPath = response.data.path;
var win = window.open(excelPath, '_blank');
if (win) {
        //Browser has allowed it to be opened
        win.focus();
} else {
        //Browser has blocked it
        swal(   
            "Error!",   
           'Please allow popups for this website',   
           "error"
        );
    }                        
}

Try and see if this works.

Akshay Singh
  • 93
  • 10