Monday, 16 March 2015

Java program for Convert CSV into excel file



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

import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class CsvToXsl {
   
    private static DataInputStream myInput;
    private static XSSFWorkbook hwb;
    private static Logger log = Logger.getLogger(
    ResultGenerator.class.getName());

    @SuppressWarnings("deprecation")
public static void readCsvToXsl(String fullPath) throws IOException{
        File f = new File(fullPath +".csv");
        File f1 = new File(fullPath +".xlsx");
        if(f.exists() && !f1.exists()){
        try {
        log.info("Inside method readCsvToXsl for Generation");
        {
           
            ArrayList> arList=null;
            ArrayList al=null;
            String thisLine;
             FileInputStream fis = new FileInputStream(fullPath +".csv");
            myInput = new DataInputStream(fis);
            arList = new ArrayList>();
            while ((thisLine = myInput.readLine()) != null)
            {
             al = new ArrayList();
             String strar[] = thisLine.split(",");
             for(int j=0;j             {
             al.add(strar[j]);
             }
             arList.add(al);
            }

            try
            {
            hwb = new XSSFWorkbook ();
             XSSFSheet sheet = hwb.createSheet("new sheet");
              for(int k=0;k              {
               ArrayList ardata = (ArrayList)arList.get(k);
               Row row = sheet.createRow((short) 0+k);
               for(int p=0;p               {
                Cell 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);
                }
               }
              }
             FileOutputStream fileOut = new FileOutputStream(fullPath+".xlsx");
             hwb.write(fileOut);
             fileOut.close();
             log.info("Your excel file has been generated");
            } catch ( Exception ex ) {
                 ex.printStackTrace();
            }
            }
        }
       
        catch (IOException e) {
            e.printStackTrace();
            log.info("unbale to find csv file at path");
        }
      }
    }

}

No comments: