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 java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.*;
import java.io.FileReader; 
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) 
                                      .withCSVParser(parser) 
                                      .build(); 
       
            
            String[] line;
            int r = 0;
            while ((line = csvreader.readNext()) != null) {
                Row row = sheet.createRow((short) r++);

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


                       .setCellValue(helper.createRichTextString(line[i]));
            }

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

        
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
   

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

   
        } 


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

    }


}





      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.