Skip to toolbar

File Handling | Reading data from Excel files (.xls or .xlsx) using JAVA.

Reading data from Excel files (.xls or .xlsx) using JAVA.

 

package excel;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.commons.io.FilenameUtils;
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.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelHandling 
{

	public static void main(String[] args) throws IOException 
	{
		// TODO Auto-generated method stub
		String filePath="input_Excel//testData.xlsx";
		loadFile(filePath);
	}
	
	public static void loadFile(String filePath) throws IOException
	{
		File file=new File(filePath);                               // Creating File Object
		String fileExtension=FilenameUtils.getExtension(filePath);  // Getting extension of file
		int totalRows=0;
		Iterator rowIterator=null;
		
		if(fileExtension.equalsIgnoreCase("xlsx"))
		{
			FileInputStream fis=new FileInputStream(file); 
			XSSFWorkbook doc =new XSSFWorkbook(fis); 
			XSSFSheet sheet=doc.getSheet("Sheet1");
		    totalRows=sheet.getLastRowNum();
		    rowIterator=sheet.rowIterator();
		}
		else if(fileExtension.equalsIgnoreCase("xls"))
		{
			FileInputStream fis =new FileInputStream(file);
			HSSFWorkbook doc=new HSSFWorkbook(fis);
			HSSFSheet sheet=doc.getSheet("Sheet1");
			totalRows=sheet.getLastRowNum();
			rowIterator=sheet.rowIterator();
		}
		 
			System.out.println("Total Row Count : "+totalRows);
			System.out.println("\nContent from document =>");
			while(rowIterator.hasNext())
			{
				Iterator cellIterator=rowIterator.next().cellIterator();
				while(cellIterator.hasNext())
				{
					String cellData=cellIterator.next().toString();
					System.out.print(cellData+"\t");
				}
				System.out.println();
			}
	}
	

}


Also, read How to read word document file using JAVA?

Other Blogs: 
https://agilehelpdoc.blogspot.in/

2537total visits,14visits today

One thought on “File Handling | Reading data from Excel files (.xls or .xlsx) using JAVA.

  1. Thank you a lot for sharing this with all of us you really know what you’re speaking approximately! Bookmarked. Kindly also discuss with my web site =). We can have a hyperlink alternate contract among us!

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 1
  • Page views today : 1
  • Total visitors : 48,241
  • Total page view: 66,388

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.