Creating CSV to EXCEL file in java

import java.io.FileOutputStream;
import java.io.FileReader;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import au.com.bytecode.opencsv.CSVReader;

public class CreateCSVToExcel {
public void createCSVTOELs(String sourcePath,String destinationPath) throws Exception
{

            Workbook wb = new HSSFWorkbook();
            CreationHelper helper = wb.getCreationHelper();
            Sheet sheet = wb.createSheet("new sheet");

            CSVReader reader = new CSVReader(new FileReader(sourcePath));
            String[] line;
            int r = 0;
            while ((line = reader.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(destinationPath);
            wb.write(fileOut);
            fileOut.close();
        
    }

}



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because 'blog posts' (or any category) is not suitable for code snippets.

You can contact us on Discord.
[utopian-moderator]