Exporting Text Data From Javascript to Excel

This note describes how to export data from Javascript to Excel using the Java Servlet API on Tomcat but should easily generalise to other formats. It is assumed you are working in Eclipse or a similar IDE. Remember no documentation is 100% accurate. This procedure worked for me and as usual you cut and past at your own risk. A basic knowledge of HTML and javascript is assumed.

Exporting data is not straightforward because Javascript does not allow writing to the client machine. If it did there would be a massive security risk. It is therefore necessary to send the data to a server and have the server send it back. The Java servlet API provides an easy way to do this, though it is possible to use other technologies such as PHP.

The problem can be broken into the following steps

1. Create and Test the Servlet

    1.1 Modify the web.xml file for the application where the data export is needed.

    You need to insert the following lines with the other servlet mappings

        ExportServlet

        package.name.for.ExportServlet

    ExportServlet

    /ExportServlet

    1.2 Write the Export servlet as a Hello World servlet in the folder corresponding to package package.name.for.

    public class ExportServlet extends HttpServlet

    {

      public void doGet(HttpServletRequest request,

         HttpServletResponse response)

                 throws ServletException, IOException

          {

                                PrintWriter out = response.getWriter();

            out.println(”OK SO FAR” );

            out.flush();

            out.close();

                          }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

doGet(request, response);}

}

    1.3 Stop and restart Tomcat (IMPORTANT)

    1.4 Request the servlet from your browser.

    The url will be something like

    http:// localhost:8080//ExportServlet. You can determine context root from web.xml or more easily from the urls for other servlets in the appplication. Getting this right is the trickiest part of the process and can be extremely frustrating. Eventaully however you will see a ”OK So far” on the screen 

      1. Get the return content type right

    2.1 In doGet set the reponse type to match Excel either before or just after the printWriter is defined

    response.setContentType(”application/excel”);

    2.2 Still in the servlet build an HTML table by writing to the print writer

    out.println(”

    ”);

    out.println(”/table>”;

    2.3 Again load the servlet into the browser. All going well an Excel Spreadsheet with your table properly exported will appear. You can now save it wherever you want.

    3. Get the data from the javascript to the servlet

      3.1 The next stage is to get the date from your javascript page to the servlet.

      3.2 Format your data as an HTML table. Place this ina javascript variable theData

      3.3 Create a form in the javascript page as follows

      var table +=”

    table +=”

    table +=”method=”post”";

    table +=”

          value=”" + thedata + “”>”;

   table +=”

   value=”Export as Spreadsheet” />”;

    table +=”

    “;

3.4 Replace your test data in the servlet with the following line just before out.flush();

out.println(request.getParameter(”mydata”));

Then stop and start Tomcat

3.5 Load the javascript page and click the button

This is a slow cautious procedure with testing at each stage. But ít is faster than plungingin and fighting multiple bugs.

Leave Your Response