Gustavo Hurtado
Gustavo Hurtado

Reputation: 25

Download excel file with vue and spring

I'm trying to make an excel file with spring (Java) and then send it to Vue, so I can download the file with the web browser.

I made the excel file with a service in Java, but I don't know how and what I have to return to Vue.

@RequestMapping(value = "/getmathresume", method = RequestMethod.GET)
public FileOutputStream getMathResume()
{
   //Long code with XSSFWorkbook();
   //.
   //.
   //.
   // Finally I Write the output to a file, but I don't want to save it locally, instead, the idea
   // is send it to Front and download the file with the Browser.
    try {
        FileOutputStream fileOut = new FileOutputStream("poi-generated-file.xlsx");
        workbook.write(fileOut);
        fileOut.close();
        workbook.close();
    }catch (FileNotFoundException e) {
        System.out.println(e);
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Using the code from the accepted answer
    try {
        File file = new File("poi-generated-file.xlsx");
        Files.copy(file.toPath(), response.getOutputStream());
        String mimeType = URLConnection.guessContentTypeFromName(file.getName());
        String contentDisposition = String.format("attachment; filename=%s", file.getName());
        int fileSize = Long.valueOf(file.length()).intValue();

        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", contentDisposition);
        response.setContentLength(fileSize);
    }catch (FileNotFoundException e) {
        System.out.println(e);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

__________________________VUE CODE____________________________

    getMathResume()
    {
        axios({
            url: 'http://localhost:8081/user/getmathresume',
            method: 'GET',
            responseType: 'blob',
        }).then((response) => {
             var fileURL = window.URL.createObjectURL(new Blob([response.data]));
             var fileLink = document.createElement('a');

             fileLink.href = fileURL;
             fileLink.setAttribute('download', 'matematica.xlsx');
             document.body.appendChild(fileLink);

             fileLink.click();
        });
    },

I don't think this is a hard thing to do, but I cannot find a tutorial to do something like this. Hope someone can help me out.

EDIT: ADDED THE NEW CODE WORKING.

Upvotes: 0

Views: 2565

Answers (1)

user1251146
user1251146

Reputation: 88

@RequestMapping(value = "/getmathresume", method = RequestMethod.GET)
public void getMathResume(HttpServletResponse response) {
    try {
        File file = new File("poi-generated-file.xlsx");
        Files.copy(file.toPath(), response.getOutputStream());
        String mimeType = URLConnection.guessContentTypeFromName(file.getName());
        String contentDisposition = String.format("attachment; filename=%s", file.getName());
        int fileSize = Long.valueOf(file.length()).intValue();

        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", contentDisposition);
        response.setContentLength(fileSize);
    }catch (FileNotFoundException e) {
        System.out.println(e);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 1

Related Questions