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

I am writing a Java Excel sheet reader program (.xlsx) using Apache Poi. I can iterate over all the cells and get all the values. But I can’t get the specific cell value, say, E10. Is there any way to do this?

Please see the code below, which I used to iterate over the cells.

package application;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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())
        {
            row=(XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            while (cells.hasNext())
            {
                cell=(XSSFCell) cells.next();   
                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()+" ");
                }
                else
                {
                }
            }
            System.out.println();
        }
    }   
}
+8
source share
5 answers

For example, to get the E10 of the first sheet:

wb.getSheetAt(0).getRow(9).getCell(4); 

Note: subtract one of them because the indices are based on zero.

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

wb.getSheetAt(0).getRow(9).getCell(CellReference.convertColStringToIndex("E"));
+21
source

XSSFSheet getRow (int rownum) ( 0). , , . , 4 .

, getCell (int cellnum) XSSFRow. (0).

+2

excel, .

wb.getSheetAt(0).getRow(1).getCell(1);
+2
public class XmlFileRead {

public static void main(String[] args) throws IOException {
    FileInputStream fi = new FileInputStream("abc.xls");
    ArrayList<EmployeeVo> al = new ArrayList<>();
    EmployeeVo evo = null;
    Scanner scanner = null;

    Workbook wb = new XSSFWorkbook(fi);
    Sheet sh = wb.getSheet("Sheet0");
    int starRow = sh.getFirstRowNum();
    int endRow = sh.getLastRowNum();

    for (int i = starRow + 1; i < endRow; i++) {
        scanner = new Scanner(System.in);
        evo = new EmployeeVo();
        Cell c = wb.getSheetAt(0).getRow(i).getCell(1);
        evo.setEmployeeId((int) c.getNumericCellValue());

        Cell c2 = wb.getSheetAt(0).getRow(i).getCell(2);
        evo.setEmployeeName(c2.toString());
        // add to collection
        al.add(evo);
    } // for

    al.forEach(i -> {
        System.out.println(i.getEmployeeId() + " " + i.getEmployeeName());
    });

}
}
+1

getCell getCell

public XSSFCell getCell(String cellName){
    Pattern r = Pattern.compile("^([A-Z]+)([0-9]+)$");
    Matcher m = r.matcher(cellName);
    XSSFWorkbook wb = new XSSFWorkbook();
    if(m.matches()) {
        String columnName = m.group(1);
        int rowNumber = Integer.parseInt(m.group(2));
        if(rowNumber > 0) {
            return wb.getSheetAt(0).getRow(rowNumber-1).getCell(CellReference.convertColStringToIndex(columnName));
        }
    }
    return null;
}

getCell("E10")
0

All Articles