All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Read Excel File In Java

Last Updated : Mar 11, 2024

Read Excel File In Java

In this article we will show you the solution of read excel file in java, in case you using eclipse then each program adds default package or basic package itself at the first line.

To achieve desired result everyone need to download and import many package during project creation otherwise you will get error.

Generally, three different type packages should need for this type of program such as util, io, and

Step By Step Guide On Read Excel File In Java :-

Main class is ‘Readexcel’, there we provide definition for public static main method.

Iterator helps us to iterate the cell and row of specific excel sheet and FileInputStream points the excel file location.

Then getting exceptions verified by IOException.

The apache.poi is an java api to access the Microsoft documents we need its support of various packages.

We pointing directly first cell and displaying having data cells value by iterator object. This process repeated up to reach empty cells.

//Read excel (.xlsx) File
package Readexcel;
import java.util.Iterator;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Readexcel {
 public static void main(String[] args) {
  String pth="C:\\Users\\welcome\\Desktop\\file.xlsx";
   FileInputStream inp=new FileInputStream(pth);
   Workbook wb=new XSSFWorkbook(inp);
   Sheet fs=wb.getSheetAt(0);
   Iterator<Row> ritr=fs.iterator();;//skip the header row
    Iterator<Cell> citr=nr.cellIterator();
     System.out.println(nc);//prints index of column
  catch(IOException ex){
   System.out.println("error occurred in reading file");
  1. The ‘Package Readexcel’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. First displaying package will inserted by eclipse to support java codes, if you try codes in eclipse platform each time it will imported after successful creation of java project.
  3. In main class ‘Readexcel’, ensure whether you are given name for class and saved program name same or not. If same you do not get confuse at any case during execution otherwise name for execution same for both run and compiling comment.
  4. Within main class we provide main method definition then foremost we giving excel path to string variable ‘pth’ and our excel file name is ‘file.xlsx’.
  5. Here we are handling exceptions, so we created try, catch blocks. In try we passing file path to ‘FileInputStream’ object ‘inp’ creation. Then we passing to representation of workbook interface variable ‘inp’.
  6. In the excel we need to point out first sheet, so we did by line getSheetAt(0) when appending it with workbook variable ‘wb’. Row iterator ‘ritr’ created by binds sheet ‘fs’ with iterator() method and we ignoring header line of excel by row iterator ‘ritr’ appends with next() method.
  7. The hasNext() traverse to next cell or row that varied when you appending variable. Here first we need to append it with row iterator ‘ritr’ then it returns the first row input. To move further we creating another iterator for cells ‘citr’ and doing steps like what we did to row iterator. Within while loop we printing retrieved result of cell values side by side.
  8. If you are file not exist or not read properly printed text ‘error occurred in reading file’ as output with line number of error file details, which is get by printStackTrace() method.
  9. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  10. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to read a excel file in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Readexcel‘ line for gets the output.

When you run the program by simply clicks on run button on eclipse will display output of fetched excel cell values from your desired excel file of ‘new.xlsx’.

I hope this article on read excel file in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪