jsf 中的文件保存对话框

发布于 2024-12-26 18:46:59 字数 248 浏览 0 评论 0原文

我正在尝试以 Excel 格式导出数据库的结果集。我正在使用 JSF 开发一个 Web 应用程序。所以我需要的是,当用户单击页面中的“导出”按钮时,必须打开一个文件保存对话框,供用户选择目录路径和文件名称。

当在文件对话框中单击“保存”时,我需要根据用户输入的名称和路径将整个结果集写入 Excel 工作表。

但我找不到文件保存对话框以及如何在 JSF 中使用它。我正在使用 jdeveloper 进行开发。

请给我建议一个解决方案。

I am trying to export the result set of the DB in a excel format. And I am developing a web application using JSF. So what i need is that when the user clicks on the EXPORT button in my page , then a file save dialog has to open for the user to choose the directory path and the name for the file.

And when the save is clicked in the file dialog then i need to write the whole result set to the excel sheet with the mentioned name and path as per users input.

But i couldnt find the file save dialog and how to use the same with JSF. And I am using jdeveloper for the development .

Please suggest me a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此岸叶落 2025-01-02 18:46:59

关键是 Content-Disposition 响应标头,您需要将其设置为 attachment。这将在浏览器中强制显示“另存为”对话框。您需要做的就是设置正确的 Content-Type 响应标头(以便浏览器知道它是什么类型的文件)并将原始文件的内容写入响应正文。

因此,总结一下(假设您使用 Apache POI 生成 Excel 工作表):

public void downloadReport() throws IOException {
    // Prepare Excel file to be downloaded.
    HSSFWorkbook workbook = generateReportSomehow();
    String filename = "yourDefaultFilename.xls";

    // Prepare response.
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/vnd.ms-excel");
    externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

    // Write file to response body.
    workbook.write(externalContext.getResponseOutputStream());

    // Inform JSF that response is completed and it thus doesn't have to navigate.
    facesContext.responseComplete();
}

The key is the Content-Disposition response header which you need to set to attachment. This will force a Save As dialogue in the browser. All you need to do along this is to set a proper Content-Type response header as well (so that the browser know what type of file it is) and to write the raw file's content to the response body.

So, summarized (assuming that you're using Apache POI to generate the Excel sheet):

public void downloadReport() throws IOException {
    // Prepare Excel file to be downloaded.
    HSSFWorkbook workbook = generateReportSomehow();
    String filename = "yourDefaultFilename.xls";

    // Prepare response.
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseContentType("application/vnd.ms-excel");
    externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

    // Write file to response body.
    workbook.write(externalContext.getResponseOutputStream());

    // Inform JSF that response is completed and it thus doesn't have to navigate.
    facesContext.responseComplete();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文