Home » excel » How to read data from excel sheet using java?

How to read data from excel sheet using java?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am a beginner in Java coding and would like to know how to read the following excel sheet data using Java.

Spreadsheet sample

Also, I have tried the below code –

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


public class ReadingFromExcelSheet {

    public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
        FileInputStream ip = new FileInputStream("C:\Users\Sanjana Rajeev\Desktop\Murali_YoutubeLinks.xlsx");
        Workbook wb = WorkbookFactory.create(ip);
        Sheet sheet = wb.getSheet("MySheet1");
        int i,j;
        int rowcount =3,cellcount=2;
        for ( i=0;i<=rowcount;i++){
        for (j=0;j<cellcount;j++){
        Row row = sheet.getRow(i);
        Cell cell = row.getCell(j);
        String cellval = cell.getStringCellValue();
        System.out.println(cellval + "\t\t" );
        }       
        }

        ip.close();
    }


}

And i am getting the below shown output :

Topics      
YouTube Links       
Java Execution
Java and JDK dowload
Eclipse download
Create a Workspace/Project/Package/Class files      
        
Java Execution
Java and JDK dowload
Eclipse download
Create a Workspace/Project/Package/Class files
Datatypes
Variables
String Concatenation        
        
Global Variables (Static & NonStatic)
Local Variables
Memory Allocation

I am getting the values of the cell but not in their proper order as like the excel sheet. Can anyone please help?

How to&Answers:

Does this improve the formatting?

    for ( i=0;i<=rowcount;i++)
    {
            Row row = sheet.getRow(i);

        for (j=0;j<cellcount;j++)
        {
            Cell cell = row.getCell(j);
            String cellval = cell.getStringCellValue();
            System.out.print(cellval + "\t\t" );
        }
        System.out.println();
    }

Answer:

Try this approach

public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
        FileInputStream ip = new FileInputStream("C:\Users\Sanjana Rajeev\Desktop\Murali_YoutubeLinks.xlsx");
        Workbook wb = WorkbookFactory.create(ip);
        Sheet sheet = wb.getSheet("MySheet1");
        Iterator<Row> rowIterator = sheet.rowIterator();
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            //iterate over the columns of the current row
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellValue = dataFormatter.formatCellValue(cell);
                System.out.print(cellValue + "\t");
            }
            //append empty line
            System.out.println();
        }

        ip.close();
    }