0

My Java REST service(POST call) is providing an excel file as response using hssfWorkbook and returning excel in xls format.

response.getOutputStream();
hssfWorkbook.write(out);

I have tried Filesaver but it will work for JSON as reponse. I didn't find any way to implement excel file download in angular JS. Please suggest any ways to do using angular JS.

user3428736
  • 864
  • 2
  • 13
  • 33

2 Answers2

1

Try this it will work.

API

httpServletResponse.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
httpServletResponse.setHeader("Content-Disposition",
                        "attachment; filename=sample.xlsx");
workbook.write(httpServletResponse.getOutputStream());
workbook.close();
httpServletResponse.getOutputStream().close();

$http call

    $scope.download = function() {
           $http({
                url: '/download',
                method: "POST",
                data: $scope.pagination,
                headers: {
                    'Content-type': 'application/json'
                },
                responseType: 'arraybuffer'
            }).success(function(data, status, headers, config) {
                var blob = new Blob([data], {
                    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                });
                saveAs(blob, "Sales_Summary_Report.xls");
            }).error(function(data, status, headers, config) {

            });
    }

HTML

<button ng-click="download()"> Download Excel </button>
Sharan De Silva
  • 598
  • 1
  • 8
  • 27
  • I am getting [Object object] in my excel .I have tried the same using FileSaver but not working. – user3428736 Oct 28 '16 at 11:54
  • I have tried your example and I am getting all the response as byte in my excel. Do you have any working example for what is the return type in JAVA and little more code it would be helpful. – user3428736 Nov 02 '16 at 06:43
  • can you download the file from browser – Sharan De Silva Nov 02 '16 at 07:31
  • Yes, but the data contains bytes not with proper data. But from back-end route it is providing proper data. – user3428736 Nov 02 '16 at 07:43
  • Server will return the workbook as byte array in httpResponse. In Client side we need to save the byte array as a file. using saveAs (blob, 'filename.extension') you can create a file from the bytes. – Sharan De Silva Nov 02 '16 at 07:49
  • I am getting the data but the bytes are not getting modified and it is still coming as response object. I have used FileSaver.saveAs – user3428736 Nov 02 '16 at 10:20
  • Check the updated answer.. This is working file for me.. Please check – Sharan De Silva Nov 02 '16 at 10:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127168/discussion-between-user3428736-and-sharan-de-silva). – user3428736 Nov 02 '16 at 10:52
  • can you answer this question http://stackoverflow.com/questions/43894768/how-to-set-the-no-of-column-name-while-export-the-excel-using-angularjs – Vinoth May 10 '17 at 14:14
0

For IE you can try msSaveOrOpenBlob for other browsers hint with invisible anchor link should solve it, code like this works good for me:

$http( /*your request details */ ).
            success(function(data) {
                var blob = new Blob([data], { type: 'application/vnd.ms-excel' }),
                    fileName = 'Sales_Summary_Report.xls';
                if (window.navigator.msSaveOrOpenBlob) {
                    window.navigator.msSaveOrOpenBlob(blob, fileName);
                } else {
                    var anchor = angular.element('<a/>');
                    anchor.css({ display: 'none' });
                    angular.element(document.body).append(anchor);
                    anchor.attr({
                        href: (window.URL || window.webkitURL).createObjectURL(blob),
                        target: '_blank',
                        download: fileName
                    })[0].click();
                    anchor.remove();
                }
            })
udalmik
  • 7,838
  • 26
  • 40
  • After using the above code.I am getting all data as byte format in my excel sheet. Please suggest why the data is coming as bytes – user3428736 Nov 02 '16 at 05:54
  • I have tried your example and I am getting all the response as byte in my excel when tried from front-end but from backend if I save it looks good. If I try the request from back-end I am getting the response as byte array but if I try to save as xls format it looks good. The same scenario from front-end that if I try to add the data to blob it is passing the bytes and writing to excel. Please suggest on this. – user3428736 Nov 02 '16 at 06:51
  • @user3428736 can you answer this question http://stackoverflow.com/questions/43894768/how-to-set-the-no-of-column-name-while-export-the-excel-using-angularjs – Vinoth May 10 '17 at 14:13