【问题标题】:JSP Servlet save as popupJSP Servlet 另存为弹出窗口
【发布时间】:2013-03-16 23:42:25
【问题描述】:

我想将 excel 中的数据下载到用户选择的路径,在 jsp 中我已经给出了 ExportToExcel 按钮,所以我想要下面的场景

当用户单击 ExportToExcel 按钮然后保存为弹出窗口时,我想从那里调用 servlet 并需要在 servlet 中接收从弹出窗口中选择的文件保存路径,然后最后我想将我的数据写入excel工作表并保存到用户选择的路径。保存后,我想在另一个 jsp 页面中向用户显示一条消息。

【问题讨论】:

    标签: jsp servlets popup save


    【解决方案1】:

    文件保存路径的信息不会以任何方式发送到服务器。另外,您显然无法使用例如new File(savedPath) 在服务器中,当客户端在物理上不同的机器上运行时,就像在非开发环境中发生的那样。所以你的整个要求没有任何意义。您应该直接将文件写入触发 Save As 对话框的同一 HTTP 响应的 HTTP 响应正文。

    不清楚您使用什么来生成 Excel 文件,但如果它是例如Apache POI,那么它看起来像这样:

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet();
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    cell.setCellValue("cell value");
    
    response.setContentType("application/vnd.ms-excel"); // Tell browser what content type the response body represents,  so that it can associate it with MS Excel, if necessary.
    response.setHeader("Content-Disposition", "attachment; filename=name.xls"); // Force "Save As" dialogue.
    workbook.write(response.getOutputStream()); // Write created Excel sheet to response. This will be saved in the location specified by the user.
    

    【讨论】:

      猜你喜欢
      • 2012-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 2012-12-12
      • 1970-01-01
      • 2018-05-10
      相关资源
      最近更新 更多