Java Poi does not download

Asked

Viewed 219 times

0

I’m using the poi 3.15 library to generate excel on a web system, but I’m having trouble downloading the file, locally it works but when I publish it does nothing. I put it at the beginning like this:

String  caminho   = System.getProperty("user.home");
String  filename  = caminho + "/arquivo.xls";

In the end:

try (FileOutputStream fileOut = new FileOutputStream(filename)) {
    workbook.write(fileOut);
}

and I call in:

case "EXPORTAR":
    new ClasseDAO().exportarExcel(id);
    dispatcher = request.getRequestDispatcher("pagina.jsp");
    dispatcher.forward(request, response);
break;

The call is in a form action, already tried setting Contenttype with vnd.ms-excel, Header with Attachment but does not work.

1 answer

1

You can write directly on the Sponse request. Ex.:

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename=qrquivo.xls");

wb.write(response.getOutputStream());
response.getOutputStream().flush();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.