Convert csv to xls/xlsx using Apache poi?

csv to excel java example
apache poi read csv
read csv file in java
convert csv to excel android
apache poi csv
how to convert text file into excel using java
read csv file in java using scanner
csv to java object

I need to convert csv to xls/xlsx in my project? How can i do that? Can anyone post me some examples? I want to do it with Apache poi. I also need to create a cell from java side.

You can try following method to create xlsx file using apache-poi.

public static void csvToXLSX() {
    try {
        String csvFileAddress = "test.csv"; //csv file address
        String xlsxFileAddress = "test.xlsx"; //xlsx file address
        XSSFWorkbook workBook = new XSSFWorkbook();
        XSSFSheet sheet = workBook.createSheet("sheet1");
        String currentLine=null;
        int RowNum=0;
        BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
        while ((currentLine = br.readLine()) != null) {
            String str[] = currentLine.split(",");
            XSSFRow currentRow=sheet.createRow(RowNum);
            for(int i=0;i<str.length;i++){

        FileOutputStream fileOutputStream =  new FileOutputStream(xlsxFileAddress);
    } catch (Exception ex) {
        System.out.println(ex.getMessage()+"Exception in try");

Java Convert Csv to Excel File Example. Hello readers, in this tutorial, we are going to implement the Csv to Excel file conversion by using the Apache POI library. This tutorial will show developers how to write large data to an excel file using SXSSF . pom.xml. pom.xml. This example shows how to convert XLSX/Excel file to CSV format using Apache POI. 1) Dependencies. Add the following highlighted dependencies to your pom.xml

We can use SXSSF Jar in which we can parse a long file as below:

public static void main( String[] args ) {
  try {

    //   String fName = args[ 0 ];

    String csvFileAddress = "C:\\Users\\psingh\\Desktop\\test\\New folder\\GenericDealerReport - version6.txt"; //csv file address
    String xlsxFileAddress = "C:\\Users\\psingh\\Desktop\\trial\\test3.xlsx"; //xlsx file address

    SXSSFWorkbook workBook = new SXSSFWorkbook( 1000 ); sheet = workBook.createSheet( "sheet1" );
    String currentLine = null;
    int RowNum = -1;
    BufferedReader br = new BufferedReader( new FileReader( csvFileAddress ) );
    while ( ( currentLine = br.readLine() ) != null ) {
      String str[] = currentLine.split( "\\|" );
      Row currentRow = sheet.createRow( RowNum );
      for ( int i = 0; i < str.length; i++ ) {
        currentRow.createCell( i )
                .setCellValue( str[ i ] );
    DateFormat df = new SimpleDateFormat( "yyyy-mm-dd-HHmmss" );
    Date today = Calendar.getInstance()
    String reportDate = df.format( today );
    FileOutputStream fileOutputStream = new FileOutputStream( xlsxFileAddress );
    workBook.write( fileOutputStream );
    //System.out.println( "Done" );
  catch ( Exception ex ) {
    System.out.println( ex.getMessage() + "Exception in try" );

Apache POI How To Convert CSV To Excel Using SuperCSV HSSFWorkbook​(); //You can use XSSFWorkbook to generate .xlsx files FileOutputStream fileOut;​  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Convert *.XLSX to .XLS using apache poi

I have found SXSSFWorkbook really faster then XSSFWorkbook. Here is the modified code:

try {
        String csvFileInput = "inputFile.csv";
        String xlsxFileOutput ="outputFile.xls";

        LOGGER.error( xlsxFileOutput);
        SXSSFWorkbook workBook = new SXSSFWorkbook();
        Sheet sheet = workBook.createSheet(transformBean.getOutputFileName());
        String currentLine = null;
        int RowNum = 0;
        BufferedReader br = new BufferedReader(new FileReader(csvFileInput));
        while ((currentLine = br.readLine()) != null) {
            String str[] = currentLine.split(",");
            Row currentRow = sheet.createRow(RowNum);
            for (int i = 0; i < str.length; i++) {
        FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileOutput);
    } catch (Exception ex) {
        System.out.println(ex.getMessage() + "Found Exception");

Convert csv to xls/xlsx (using Apache POI, openCSV). I had a task to make "csv to xlsx" converter for reason nonfunctional M$ Excel import tool. Java - Apache POI - Convert XLS/XLSX to CSV. GitHub Gist: instantly share code, notes, and snippets.

public static void convertCsvToXlsx(String xlsLocation, String csvLocation) throws Exception {
    SXSSFWorkbook workbook = new SXSSFWorkbook();
    SXSSFSheet sheet = workbook.createSheet("Sheet");
    AtomicReference<Integer> row = new AtomicReference<>(0);
    Files.readAllLines(Paths.get(csvLocation)).forEach(line -> {
        Row currentRow = sheet.createRow(row.getAndSet(row.get() + 1));
        String[] nextLine = line.split(",");
        Stream.iterate(0, i -> i + 1).limit(nextLine.length).forEach(i -> {
    FileOutputStream fos = new FileOutputStream(new File(xlsLocation));

Program to convert CSV file to XLS using java org.apache.poi API. Download org​.apache.poi package from  Joel penned the article in 2003, long after leaving the Excel group. Using Apache POI. We use Apache POI to read the Excel spreadsheet. Building the program requires this dependency to be declared

if(new File(newFileName).isFile()) return;
    HSSFWorkbook wb = new HSSFWorkbook();
    Row xlsRow;
    Cell xlsCell;
    HSSFSheet sheet = wb.createSheet("sheet1");
    int rowIndex = 0;
    for(CSVRecord record : CSVFormat.EXCEL.parse(new FileReader(fileName))) {
        xlsRow = sheet.createRow(rowIndex);
        for(int i = 0; i < record.size(); i ++){
            xlsCell = xlsRow.createCell(i);
        rowIndex ++;
    FileOutputStream out = new FileOutputStream(newFileName);

Using Java API read Comma Separated Values(.csv) file and convert into XLS file with Apache POI API. * <p>. * Microsoft Excel file is converted  6 Responses to Convert Excel to delimited text or Csv Using Apache POI. /Downloads/source.xlsx”; From Where I Get Apache POI..For Convert Excel Sheet In To

In this article, I am going to explain to you how to convert csv file to and using Apache POI library I am creating new sheet in the excel file and  In your solution iteration is available. Instead of iterate from xlsx to csv,I can directly read from xlsx using Apache Poi(XSSF)and write into database. I need any other direct way to convert xlsx to csv using java sir. Regards, Tharanya B – Tharani Oct 27 '16 at 11:46

This tutorial explains how to convert CSV File to XLS Format using Apache POI / OpenCSV in Java with Examples. In the tutorial, Grokonez shows how to convert Excel File to CSV File and vice versa with Java language by examples. ContentsOverviewConvert Excel File to CSV FileConvert CSV File to Excel File Overview To convert Excel File to/from CSV File and vice versa, We use 2 dependencies: Apache POI Apache Commons -> Details: [crayon-5ec8d3a95776a022736549/] – …

This is a C library, but could be bridged to just about anything, I use it with Swift for example, it works great. For Java, try Apache POI… Apache POI - the Java API​  Convert CSV File to Excel in Java Example In this tutorial, we will explain how to convert a CSV file to XLS format in Java . We will be using OpenCSV [opencsv-2.3.jar] and Apache POI [v3.8] to convert a CSV file to XLS format.

  • thanks but my csv is | delimited. when i replace the comma with pipi i'm getting one character per cell. what should i do??
  • Please confirm you have used simply "|" or "\\|".
  • got it,, i was using "|" only which meant logical or, i should have used "\\|". thanks
  • This throws an exception on line: "XSSFWorkbook workBook = new XSSFWorkbook();," saying "NoClassDefFoundError."
  • You should follow the naming conventions. That means rowNum instead of RowNum. Also, String[] str is preferred rather than String str[], see JLS 7.
  • This is the FASTEST solution and is clearly self documented. In my tests this is 30% faster than the the solution above from Superb Self.