How to convert CSV file into ExcelSheet


This   is    vary     useful   java code  , this  code   can be use  to file   convert  .csv file   into excel sheet.  Requirement  to run this  code  in your machine  is  java   poi   package ,  opencsv jar 4.6 
and  other  jars . 

                        i   have  executed  this  code  in my  system . 

package Java8;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.util.List; 
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVParser;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;

public class CSVtoexcelconvertor {
    public static void readDataFromCustomSeperator(String file) 
     Workbook wb = new HSSFWorkbook();
         CreationHelper helper = wb.getCreationHelper();
         Sheet sheet = wb.createSheet("new sheet");

        try { 
            FileReader filereader = new FileReader(file); 
            // create csvParser object with 
            // custom seperator semi-colon 
            CSVParser parser = new CSVParserBuilder().withSeparator(';').build(); 
            // create csvReader object with 
            // parameter filereader and parser 
            CSVReader csvreader = new CSVReaderBuilder(filereader) 
            String[] line;
            int r = 0;
            while ((line = csvreader.readNext()) != null) {
                Row row = sheet.createRow((short) r++);

                for (int i = 0; i < line.length; i++)


            // Write the output to a file
            FileOutputStream fileOut = new FileOutputStream("workbook.xls");
            System.out.print("File created");

        catch (Exception e) { 

      /**      List allData = csvReader.readAll();          
        // print Data 
            for (String[] row : allData) { 
                for (String cell : row) { 
                    System.out.print(cell + "\t"); 
        catch (Exception e) { 
        } **/


         public static void main(String[] args) throws IOException {
  String SV_FILE_CUSTOM_SEPERATOR="/Users/macbook/Documents/Epath/CSVEm.rtf" ;



      When  you  able to run this  code   .  you get  excel sheet  like this . 

       with  message  File created .

How to convert CSV file into ExcelSheet  How  to  convert  CSV   file  into  ExcelSheet Reviewed by Mukesh Jha on 9:45 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.