Read all type of excel cell value as String using POI

This example shows you how you can get the any type of cell value of excel as String using Apache POI.

The excel cell can have many types like String, Numeric, Dates, Boolean etc, But sometime you want to read the cell values as String.

Below is the example of reading an excel file and reading the different types of cell as String. We have written one method which take POI Cell object as parameter and return the String value of cell, Different type if String format of the value you can defined as you want, For example in this example we have used the Date format as “dd/MM/yyyy”.

package com.java.connect.poi;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

// Get all type of excel cell value as String using POI
public class POIReadingCellValueAsStringExample {

	public static void main(String[] args) throws Exception {
		// Get the input stream of excel file
		InputStream inputStream = new FileInputStream(
				"POIAddDifferentTypeCellsExample.xls");
		// Create a workbook object.
		Workbook wb = WorkbookFactory.create(inputStream);
		Sheet sheet = wb.getSheetAt(0);
		// Iterate over all the row and cells
		for (Iterator<Row> rit = sheet.rowIterator(); rit.hasNext();) {
			Row row = rit.next();
			for (Iterator<Cell> cit = row.cellIterator(); cit.hasNext();) {
				Cell cell = cit.next();
				// Print the each cell value
				System.out.println("Output : " + getCellValueAsString(cell));
			}
		}
	}


	/**
	 * This method for the type of data in the cell, extracts the data and
	 * returns it as a string.
	 */
	public static String getCellValueAsString(Cell cell) {
		String strCellValue = null;
		if (cell != null) {
			switch (cell.getCellType()) {
			case Cell.CELL_TYPE_STRING:
				strCellValue = cell.toString();
				break;
			case Cell.CELL_TYPE_NUMERIC:
				if (DateUtil.isCellDateFormatted(cell)) {
					SimpleDateFormat dateFormat = new SimpleDateFormat(
							"dd/MM/yyyy");
					strCellValue = dateFormat.format(cell.getDateCellValue());
				} else {
					Double value = cell.getNumericCellValue();
					Long longValue = value.longValue();
					strCellValue = new String(longValue.toString());
				}
				break;
			case Cell.CELL_TYPE_BOOLEAN:
				strCellValue = new String(new Boolean(
						cell.getBooleanCellValue()).toString());
				break;
			case Cell.CELL_TYPE_BLANK:
				strCellValue = "";
				break;
			}
		}
		return strCellValue;
	}
}