Download Excel file in browser using java

We Are Going To Discuss About Download Excel file in browser using java. So lets Start this Java Article.

Download Excel file in browser using java

Advertisements
  1. Download Excel file in browser using java

    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;

  2. Download Excel file in browser using java

    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;

Solution 1

Advertisements

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.

Original Author Denis Jr Of This Content

Solution 2

Advertisements

A more “professional” solution is to use Excel Api’s for java, as Apache POI or JXL.

Here an example using JXL:

https://www.java-tips.org/other-api-tips-100035/93-jexcel/418-how-do-i-output-an-excel-file-from-a-servlet.html

And here one more using Apache POI:

https://programtalk.com/java/download-excel-using-servlet/

I hope this can helps you.
Best wishes!

Original Author Arsensys Of This Content

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Advertisements
Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment