0

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.

1
  • Is it necessary to save it as a file before sending to the client? When I write the workbook to a byte-array and send to the client, it doesn't seem to work.... Commented May 23, 2021 at 20:23

1 Answer 1

1
@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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.