QA's approach 2 Java - Apache POI

Apache POI - Java API To Access Microsoft Excel Format Files

Read Excel file Data

package helper;

import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;

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;

public class ExcelReadWrite {

static XSSFWorkbook book = null;
static XSSFSheet sheet = null;
XSSFRow row = null;
XSSFCell cell = null;

// get ExcelReadWrite object using static factory method
public static ExcelReadWrite getExcelReaderWriter() {
return new ExcelReadWrite();
}

// Method to create workbook object to work with Excel

static XSSFWorkbook getWorkbook(String filePath)

{
String path = filePath;
try {
book = new XSSFWorkbook(path);
return book;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

// Method to find number of Sheets in the WorkBook & Print the sheet names
static int getSheetCount(String filePath) {

book = ExcelReadWrite.getWorkbook(filePath);
int count = book.getNumberOfSheets();

// Read the Sheet name by iterating over the Sheet count
while (count != 0) {
System.out.println(count + ". Sheet Name is " + book.getSheetName(count - 1));
count--;
}
return count;

}

// Method to Create an object for the sheet to work on the sheet at
// specified index

static XSSFSheet getSheet(String filePath, int index) {
book = ExcelReadWrite.getWorkbook(filePath);
sheet = book.getSheetAt(index);
return sheet;
}

// Method to read the data in the specified excel & print to console

static void excelReader(String filePath, int index) {
book = ExcelReadWrite.getWorkbook(filePath);
sheet = book.getSheetAt(index);
XSSFRow row;
XSSFCell cell;

Iterator rows = sheet.rowIterator();
System.out.println("Data in the sheet is as follows:");

// Verify if there is data in next row & iterate if available

while (rows.hasNext()) {
row = (XSSFRow) rows.next();

Iterator cells = row.cellIterator();

// Verify if the next column had data & iterate if available

while (cells.hasNext()) {
cell = (XSSFCell) cells.next();
if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
System.out.print("/t");
}

if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue() + "/t");
}

if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
System.out.print(cell.getStringCellValue() + "\t");
}

if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
System.out.print("Value in the Cell is " + cell.getBooleanCellValue());
}

}
System.out.println();

// Closing the resources

try {
ExcelReadWrite.getWorkbook(filePath).close();
} catch (Exception e) {
e.printStackTrace();
}

}

}

public static void main(String... args) {
String path = "C:\\Users\\user\\Desktop\\WorkBook2.xlsx";
ExcelReadWrite.excelReader(path, 0);
}

}

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

QA's approach 2 Java - Collections Framework & Generics Introduction

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?