【问题标题】:Download excel file with vue and spring用vue和spring下载excel文件
【发布时间】:2020-01-20 02:49:12
【问题描述】:

我正在尝试使用 spring (Java) 制作一个 excel 文件,然后将其发送到 Vue,以便我可以使用 Web 浏览器下载该文件。

我使用 Java 中的服务制作了 excel 文件,但我不知道如何以及必须返回 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 代码________________________

    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();
        });
    },

我不认为这是一件难事,但我找不到一个教程来做这样的事情。 希望有人能帮帮我。

编辑:添加了新的工作代码。

【问题讨论】:

  • 发送给客户端前是否需要保存为文件?当我将工作簿写入字节数组并发送给客户端时,它似乎不起作用....

标签: java excel vue.js axios


【解决方案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();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-17
    • 2019-12-03
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2014-12-08
    相关资源
    最近更新 更多