如何从服务器下载生成Excel工作簿?

问题描述 投票:0回答:1

我从产生的Apache POI服务器上的Excel文件。而不是写我要下载的文件的服务器上。下面的代码是写在服务器上的文件。我想修改它,为了直接下载该文件。

try (FileOutputStream outputStream = new FileOutputStream(path+"filename.xlsx")) {
    workbook.write(outputStream);
}catch (Exception e){
    e.printStackTrace();
}
java-ee apache-poi
1个回答
1
投票

我想你想要做的是这样的

public class A extends HttpServlet {
    @Override
    protected void service(HttpServletRequest rq, HttpServletResponse rs) {
        // ...
        try {
            rs.setContentType("application/vnd.ms-excel");
            workbook.write(rs.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } 
        // ...
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.