CSV to XLS converter : Java in Java

Program to convert CSV file to XLS using java org.apache.poi API.

Download org.apache.poi package from http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.8-20120326.zip


package CSVToExcel;


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;


public class CSVToExcelConverter {

    public static void main(String args[]) throws IOException
    { 
    ArrayList arList=null;
    ArrayList al=null;
    String fName = "test.csv";
    String thisLine; 
    int count=0; 
     FileInputStream fis = new FileInputStream(fName);
     DataInputStream myInput = new DataInputStream(fis);
    int i=0;
    arList = new ArrayList();
    while ((thisLine = myInput.readLine()) != null)
    {
     al = new ArrayList();
     String strar[] = thisLine.split(",");
     for(int j=0;j<strar.length;j++)
     {
     al.add(strar[j]);
     }
     arList.add(al);
     System.out.println();
     i++;
    } 

    try
    {
     HSSFWorkbook hwb = new HSSFWorkbook();
     HSSFSheet sheet = hwb.createSheet("new sheet");
      for(int k=0;k<arList.size();k++)
      {
       ArrayList ardata = (ArrayList)arList.get(k);
       HSSFRow row = sheet.createRow((short) 0+k);
       for(int p=0;p<ardata.size();p++)
       {
        HSSFCell cell = row.createCell((short) p);
        String data = ardata.get(p).toString();
        if(data.startsWith("=")){
         cell.setCellType(Cell.CELL_TYPE_STRING);
         data=data.replaceAll("\"", "");
         data=data.replaceAll("=", "");
         cell.setCellValue(data);
        }else if(data.startsWith("\"")){
            data=data.replaceAll("\"", "");
            cell.setCellType(Cell.CELL_TYPE_STRING);
            cell.setCellValue(data);
        }else{
            data=data.replaceAll("\"", "");
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
            cell.setCellValue(data);
        }
        //*/
     //   cell.setCellValue(ardata.get(p).toString());
       }
       System.out.println();
      } 
     FileOutputStream fileOut = new FileOutputStream("test.xls");
     hwb.write(fileOut);
     fileOut.close();
     System.out.println("Your excel file has been generated");
    } catch ( Exception ex ) {
         ex.printStackTrace();
    } //main method ends
    } 
}