Convert CSV to XLSX Java Servlet Example - Part 2

We are discussing how to convert comma separated value format documents to Microsoft Excel Spreadsheet format through Java Servlet API Code and this is part -2 of the tutorial series. We have so far created a HTML form, that will accept the user uploaded CSV file. In the form, we also specified a drop down element box, that accepts either XLS or XLSX. The form will post the CSV file and user selection to the servlet and the servlet will identify the request parameters, and send the desired output back to the browser. We will cover some more sections of the example in this post.

Step -2: Download required libraries for CSV to Excel Servlet Code


You need to have a copy of the following for this example to work:


  • apache-tomcat-6.0.36 (installed in your system)
  • apache-poi-v3.8 
  • opencsv-2.3
  • apache- commons-fileupload-1.2.2 (this library is required to handle uploaded file in the server)
  • apache - commons-io-2.4

There will be multiple JAR files for each of these libraries, which you can get hold of once you download these. These JAR files will be required to compile the server side code.

Step – 3:  Java Servlet Code to Convert CSV to XLS / XLSX Format

In this step, we will write servlet code that

a) Accepts the user uploaded CSV file and type
b) Uses Apache POI / OpenCSV library files to convert the CSV file to XLS / XLSX format
c) Dumps the output file back to the user

We will name the servlet Csv2XlsServlet. 

How to handle uploaded file through Servlet Code in Java?

Step-3a requires some understanding of how to handle uploaded files in the servlet code of Java. To handle the uploaded file, we will make use of apache-commons-fileupload-1.2.2 and apache-commons-ie-2.4 libraries. These libraries implement RFC2388, and we can put some simple code in the servlet to read the uploaded file. The code snippet is provided below:

                for (FileItem item : items) {
                        if (item.isFormField()) {                               
                                String fieldname = item.getFieldName();
                                if (fieldname.equals("element_2")) {
                                outputtype = item.getString();
                                }
                                //the value can be 1 for XLS conversion, 2 for XLSX conversion
                                
                        } else {
                        //The uploaded file is processed in this section
                        String fieldname = item.getFieldName();
                        String filename = FilenameUtils.getName(item.getName());
                        filecontent = item.getInputStream();
                        //Uploaded file is obtained into Inputstream object at this step                
                        }
                }

What we have done so far is, we have produced a clipped version of servlet code, that loops through all the form field elements and detects what has been specified as target file type. (i.e XLS / XLSX). It also accepts a user uploaded file into an InputStream object in Java.

Great, we have completed Part -2 of this tutorial. In the next post, we will present the full Java servlet code that will do 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