All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Read Data From Excel In Java

Last Updated : Mar 11, 2024

Read Data From Excel In Java

In this article we will show you the solution of read data from excel in java, because of the cells in an excel file, reading an excel file in Java is not the same as reading a word file.

The JDK lacks a direct API for both reading and writing Microsoft Excel and Word documents.

We should indeed rely on the third-party Apache POI library. The term "implementation of XSSF" (XML SpreadSheet Format) refers to an API that is compatible with Excel 2007 and later releases.

Workbook for Interactions: It is a Microsoft Excel Workbook representation. It is an interface implemented by HSSFWorkbook and XSSFWorkbook.

Sheet: An Excel worksheet is represented by this interface. A workbook's primary component, the sheet, is indeed a grid of cells.

The Sheet interface is an extension of java.lang.Iterable.

Row: It is a separate interface that depicts a spreadsheet row. Row is an extension of java.lang.Iterable. HSSFRow and XSSFRow are two concrete classes.

Cell:It acts as a gateway. It's a skewed representation of a worksheet cell inside a row. Cell interface is implemented by HSSFCell and XSSFCell.

Step By Step Guide On Read Data From Excel In Java :-

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
public class TalkersCoderead
{
public static void main(String args[]) throws IOException
{
FileInputStream fis=new FileInputStream(new File("C:\\talkerscode.xls"));
HSSFWorkbook wb=new HSSFWorkbook(fis);
HSSFSheet sheet=wb.getSheetAt(0);
FormulaEvaluator formulaEvaluator=wb.getCreationHelper().createFormulaEvaluator();
for(Row row: sheet)
{
for(Cell cell: row)
{
switch(formulaEvaluator.evaluateInCell(cell).getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue()+ "\t\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue()+ "\t\t");
break;
}
}
System.out.println();
}
}
}
  1. In Eclipse, create a basic Java project.
  2. In the project, make a lib folder.
  3. Download and place this same following zip files in the lib folder: - commons-collections4-4.1.jar Click Here poi-3.17.jar Click Here poi-3.17.jar -ooxml-3.17.jar Click here to download poi-ooxml-schemas-3.17.jar Please Go Here xmlbeans-2.6.0.jar Please Go Here
  4. Configure the Class Path:- Right-click the project, choose Build Path, Add External JARs, select all of the aforementioned jar files, then click Apply and Close.
  5. Make a ReadExcelFile class file as well as copying and pasting the code into it.
  6. Make an excel file called "student.xls" and fill it with data.
  7. Save the programme and run it.

Conclusion :-

Microsoft Documents can be read and written in both formats using the Java API known as Apache POI.xls and.xlsx files are supported. It contains interfaces and classes.

Horrible SpreadSheet Format (HSSF) Implementation: Indicates an API that is compatible to Excel 2003 as well as earlier versions.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪