How to find number of columns from Excel using Apache POI API

How to find number of columns from Excel using Apache POI API?

How to find number of columns from Excel using Apache POI API?

In this tutorial we are going to understand how to find total number of columns in the excel sheet.

Program to find number of columns from Excel using Apache POI api

Step 1 : Importing of necessary packages

// package from Fileinput

import java.io.FileInputStream;

// packages from apache POI

import org.apache.poi.xssf.usermodel.XSSFCell;

import org.apache.poi.xssf.usermodel.XSSFRow;

import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Step 2 : To read the data from Excel, we are using FileInputStream

Path refers to location of the excelsheet.

FileInputStream ExcelFile = new FileInputStream(path);

Step 3 : Open the existing workbook instance of xlxs file

XSSFWorkbook ExcelWBook = new XSSFWorkbook(ExcelFile);

Step 4 : Access the existing worksheet of xlsx file, in this instance it is “Sheet1”

XSSFSheet ExcelWSheet = ExcelWBook.getSheet(“Sheet1”);

Step 5 : Will return the total number of columns in the excel sheet.

Return type is integer.

int totalColumns = ExcelWSheet.getRow(0).getPhysicalNumberOfCells();

Step 6 : Will print the total number of Columns in the excel sheet

System.out.println(“Total number of Columns :::”+totalColumns);

Program :

Leave a Comment

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