javaexcelapache-poioracle-adf

Download Excel file in browser using java


I have seen this question so many places, But still not able to solve my requirement.

I have written code to generate excelsheet in Apache POI in ADF using Java and need to download it in browser as application will be in server side not always in my local machine.

Initially I tried code:

Desktop.getDesktop().open(new File(home + "/Downloads/" + "excel1" + filename + ".xls"));

It was downloading. But it is downloading only on my machine. It's not downloading on other machine.

Another solution:

file = new File(home + "/Downloads/" + "excel" + filename + ".xls");
Runtime.getRuntime().exec("cmd.exe /C start " + file);

But it is not working..

Another solution:

FacesContext fc = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
response.reset();
response.setContentType(“application/vnd.ms-excel”);
response.setHeader(“Content-Disposition”, “attachment; filename=\"excel.xlsx");
workbook.write(response.getOutputStream());
fc.responseComplete();

But this solution also does not work.


Solution

  • I have a suggestion to try to solve your problem.

    I usually create a servlet that has the responsibility of downloading files in various formats: xls, pdf...

    Here is an example of how this can be done:

    import java.io.IOException;
    import java.io.OutputStream;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class DownloadServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String fileName = request.getParameter("fileName");
            String contentType = null;
    
            if (fileName.endsWith("xls")) {
                contentType = "application/octet-stream";
            } else if (fileName.endsWith("pdf")) {
                contentType = "application/pdf";
            } else {
                throw new RuntimeException("File type not found");
            }
    
            byte[] file = getFileOnServer(fileName);
    
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            response.setHeader("charset", "iso-8859-1");
            response.setContentType(contentType);
            response.setContentLength(file.length);
            response.setStatus(HttpServletResponse.SC_OK);
    
            OutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
                outputStream.write(file, 0, file.length);
                outputStream.flush();
                outputStream.close();
                response.flushBuffer();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doPost(request, response);
        }
    
        private byte[] getFileOnServer(String fileName) {
            //implement your method to get the file in byte[]
            return null;
        }
    
    }
    

    So, you can call your servlet by URL:

    http://localhost:8080/downloadServlet?fileName=myExcel.xls

    or Form:

    <form id="myDownloadServlet" action="downloadServlet" method="post">
        <input type="text" id="fileName" name="fileName" />
        <input type="submit" id="btnDownload" name="btnDownload" value="Download File" />
    </form>
    

    Don't forget to configure your web.xml or use the annotation @WebServlet.

    I hope I've helped.