Maruthi Srinivas Sikhakolli Maruthi Srinivas Sikhakolli - 1 year ago 127
Java Question

How to get value from a specific cell of an xlsx file using java apache poi

i am writing a java program to read an excel sheet (xlsx) using apache poi. I can be able to iterate over all the cells and get all the values. But i am not able to get a specific cell value, say E10.
Is there any way to find this?

Please see the code that i used for iterating over all cells.

package application;

import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

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 ReadFromXLSX {
public static void readXLSXFile() throws IOException
InputStream ExcelFileToRead = new FileInputStream("C:\\Test.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFWorkbook test = new XSSFWorkbook();
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
Iterator rows = sheet.rowIterator();
while (rows.hasNext())
Iterator cells = row.cellIterator();
while (cells.hasNext())
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
System.out.print(cell.getStringCellValue()+" ");
else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
System.out.print(cell.getNumericCellValue()+" ");

Answer Source

For example, to get E10 of the first worksheet:


Note: subtract one because the indices are null-based.

You can also use this convenience method to map E to 4.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download