Convert CSV to XLSX Java Servlet Example - Part 1

We have been discussing CSV to Excel file conversion in Java at length in the past tutorials. In this set of posts, we will provide a servlet example for doing this conversion. Ideally, we want users to upload a CSV file through a web browser and specify the output as XLS or XLSX format. When they hit the submit button, the server side code will accept the user uploaded file and convert it into an excel document. The Excel document will then be returned back to the client which can be opened in the browser. We need quite a number of JAR files for this to work. We will take this through step by step in this post. The high level functionalities that needs to be done for this example to work is captured below:

  • Create a HTML form to upload CSV file 
  • Download required libraries for CSV to Excel Servlet Code 
  • Write Java Servlet code to create Excel File from CSV Input 
  • Copy required JAR files to support conversion into Server (Apache TomCat) 
  • Configure server side code to support conversion 
  • Change HTML form code to invoke Servlet

We will take through each of these steps in detail below.

Step-1: Prepare HTML Form – CSV to XLS Servlet

You can build the form anyway you like. I want to keep things simple, so, here is a basic HTML code that generates the form for this example.

<body id="main_body" >
        <img id="top" src="top.png" alt="">
        <div id="form_container">
                <h1><a>CSV to XLS Converter Servlet</a></h1>
                <form id="form_533088" class="appnitro" enctype="multipart/form-data" method="post" action="">
                                        <div class="form_description">
                        <h2>CSV to XLS Converter Servlet</h2>
                        <p>This is a quick form to convert CSV data to Microsoft Excel format by using a servlet. We use Apache POI and OpenCSV libraries for the conversion.</p>
                        <ul >
                                        <li id="li_1" >
                <label class="description" for="element_1">Select a CSV File to Convert </label>
                        <input id="element_1" name="element_1" class="element file" type="file"/> 
                </div> <p class="guidelines" id="guide_1"><small>In this field, you have to specify your input CSV file for upload.</small></p> 
                </li>           <li id="li_2" >
                <label class="description" for="element_2">Excel Output Format </label>
                <select class="element select medium" id="element_2" name="element_2"> 
                        <option value="" selected="selected"></option>
<option value="1" >XLS</option>
<option value="2" >XLSX</option>
<option value="3" >Third option</option>

                </div><p class="guidelines" id="guide_2"><small>You have to select
XLS - If you want the output to be converted to Excel 97-2003 format by the servlet.
XLSX - If you want the output to be converted to Excel 2007-2012 format by the servlet code.</small></p> 
                                        <li class="buttons">
                            <input type="hidden" name="form_id" value="533088" />
                                <input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
        <img id="bottom" src="bottom.png" alt="">

The generated form for the example is shown in the screenshot below: (The full form code is available for download at the end of this post )

CSV to XLS Conversion - Java Servlet Example
CSV to XLS Conversion - Java Servlet Example
We will have to make some changes to the form code for the servlet to work. We will cover that in detail as we move on. For now, we have a basic form to get things started for us.

If you are successful until this point, you can move to the next part of the tutorial, that will explain how to write the servlet code, that does the conversion.

This Series: How to Convert CSV to XLSX using Java Servlet API Code?

Keywords: Convert CSV to XLS, Convert CSV to XLSX, Java Servlet Code Example, POI, OpenCSV, Apache Common library, Apache IO

All Parts:
Part 1 - Create HTML Form Required to Support Servlet Code
Part 2-  Identify JAR files required - Write Servlet Side Code
Part 3-  Complete Servlet Code to convert CSV to XLS / XLSX in Java
Part 4-  Configure Server to support servlet - Download source files

No comments:

Post a Comment