【问题标题】:download excel generated file下载excel生成的文件
【发布时间】:2018-05-27 21:02:43
【问题描述】:

我的 html 页面上有这个按钮。 当我点击它时,exportCOAExcel 被触发。 然后生成一个excel工作簿并保存到一个路径中。

我想要一个提示来说明您要将文件下载到哪个路径?或保存到浏览器默认的“下载”文件夹位置。

@GetMapping(value = "/coaExport", params = "action=excel")
public void exportCOAExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
    XSSFWorkbook workbook = new XSSFWorkbook();
    //loogic to fill up the excel workbook with data

    FileOutputStream outputStream = new 
    FileOutputStream("C:\\Users\\user\\Desktop\\revenue.xls");
    workbook.write(outputStream);
    outputStream.close();

}

【问题讨论】:

  • 我假设您的 Web 应用程序正在服务器上运行 - 将文件保存到服务器上的特定位置没有意义 - 相反,您需要将文件发送到 HttpServletResponse 并设置正确的标头.您无法选择文件的保存位置 - 这取决于用户的浏览器。
  • 如何将文件发送到响应?

标签: java excel spring-boot


【解决方案1】:

您需要向浏览器发送内容处置响应,以便它可以理解它是要下载的文件。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

【讨论】:

    猜你喜欢
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2011-04-11
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多