简体   繁体   中英

Searching Specific Excel Cell using Java Apache POI

I am using Apache POI API to read Excel file and check the quantity of products available or not. I am successfully reading excel file using below code but I am unable to read the specific cell (Quantity Column) to check weather asked product is available or not

    package practice;
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Iterator;
    public class ReadingExcel {
    private static String brand = "Nike";
    private static String shoeName = "Roshe One";
    private static String colour = "Black";
    private static String size = "9.0";
    private static String quantity;

public static void main(String [] args){
    try {

        FileInputStream excelFile = new FileInputStream(new File("C:\\Users\\admin\\Downloads\\Project\\assets\\Shoe_Store_Storeroom.xlsx"));
        Workbook workbook = new XSSFWorkbook(excelFile);
        Sheet datatypeSheet = workbook.getSheetAt(0);
        DataFormatter dataFormatter = new DataFormatter();
        Iterator<Row> iterator = datatypeSheet.iterator();
        while (iterator.hasNext()) {
            Row currentRow = iterator.next();
            Iterator<Cell> cellIterator = currentRow.iterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellValue = dataFormatter.formatCellValue(cell);
                System.out.print(cellValue + "\t\t");
            }
            System.out.println();

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }  
}

}

this is my excel file在此处输入图片说明

using opencsv 4.1 i do this

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

...

    try (InputStream excelFile  = new FileInputStream(new File(fileName));
            Workbook wb = new XSSFWorkbook(excelFile);) {
        for (int numSheet = 0; numSheet < wb.getNumberOfSheets(); numSheet++) {
            Sheet xssfSheet = wb.getSheetAt(numSheet);

            xssfSheet.forEach(row -> {

                Cell cell = row.getCell(0);

                if (cell != null) {                     
                    if(StringUtils.isNumeric(cell.getStringCellValue())){

                        // Use here cell.getStringCellValue()
                    }
                }

            });

        }
    }

Use below for loop:

for(int r=sh.getFirstRowNum()+1; r<=sh.getLastRowNum(); r++){   //iterating through all the rows of excel
            Row currentRow = sh.getRow(r);                              //get r'th row
            Cell quantityCell = currentRow.getCell(4);                  //assuming your quantity cell will always be at position 4; if that is not the case check for the header value first
            String currentCellValue = quantityCell.getStringCellValue();
            if(currentCellValue.equalsIgnoreCase(<value you want to campare with>))
                //<do your further actions here>
            else
                //do your further actions here
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM