【问题标题】:Excel download by using apache poi使用apache poi下载Excel
【发布时间】:2017-07-21 15:03:10
【问题描述】:

我希望在用户调用此方法时下载 excel 文件。文件下载成功,但这是在项目的 classPath 中创建另一个 excel 文件。谁能帮我避免创建这个classPath文件。 提前致谢。

@Override
    public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws IOException { 

              File file = new File("Segmentdetail.xlsx"); 

              XSSFWorkbook workbook = new XSSFWorkbook(); 
              XSSFSheet spreadsheet = workbook.createSheet("SegmentLogs Info");
              spreadsheet.setDefaultColumnWidth(20);
             .....Here is the logic for generating sheet which is quite big so iam skipping it.

             }           
             FileOutputStream out = new FileOutputStream(file);
             workbook.write(out);
             downloadFile(file,response);
             out.close();
             workbook.close();

    }

    private void downloadFile(File file, HttpServletResponse response){

        try {         
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("content-disposition", "attachment; filename=Segmentdetail.xlsx");
            response.setHeader("Pragma", "public");
            response.setHeader("Cache-Control", "no-store");
            response.addHeader("Cache-Control", "max-age=0");
            FileInputStream fin = null;
            try {
                fin = new FileInputStream(file);
            } catch (final FileNotFoundException e) {
                e.printStackTrace();
            }
            final int size = 1024;
            try {
                response.setContentLength(fin.available());
                final byte[] buffer = new byte[size];
                ServletOutputStream outputStream = null;

                outputStream = response.getOutputStream();
                int length = 0;
                while ((length = fin.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, length);
                }
                fin.close();
                outputStream.flush();
                outputStream.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }catch (final Exception ex){
            ex.printStackTrace();
    }

}

【问题讨论】:

    标签: java excel spring-boot download apache-poi


    【解决方案1】:

    为您要写入 excel 的文件使用绝对路径:

    File file = new File(      "C:\\Segmentdetail.xlsx"); // windows
    File file = new File("/home/usr/Segmentdetail.xlsx"); // unix    
    

    合理的添加是使用变量:

    File file = new File(System.getenv("user.home"), "Segmentdetail.xlsx");
    

    您当然也可以定义自定义变量并使用它。

    【讨论】:

    • 我不想在本地机器上创建文件。它应该在会话中并从那里下载。
    【解决方案2】:

    因为您正在使用以下命令创建文件

    File file = new File("Segmentdetail.xlsx"); 
    

    它将在类路径中生成文件

    下载时最好给出文件路径和相同的文件路径

    File file = new File("c://Segmentdetail.xlsx"); 
    

    【讨论】:

    • 对不起,我尝试了上面的一个,但以找不到文件异常结束。顺便说一句,我使用的是 Ubuntu。
    • 然后指定路径如下 /home/usr/Filename 并在下载时提供相同的路径作为文件名 =
    • 不,它不是同样的异常。有没有办法让excel表格保持在会话中并从那里下载?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多