Поиск Specifi c Excel Cell с использованием Java Apache POI - PullRequest
0 голосов
/ 12 января 2020

Я использую Apache POI API для чтения файла Excel и проверки количества доступных продуктов или нет. Я успешно читаю файл Excel, используя приведенный ниже код, но не могу прочитать указанную ячейку c (столбец количества), чтобы проверить, доступен ли запрашиваемый продукт или нет

    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();
    }  
}

}

это мой файл Excel enter image description here

Ответы [ 2 ]

0 голосов
/ 13 января 2020

Используйте ниже для l oop:

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
        }
0 голосов
/ 12 января 2020

используя opencsv 4.1 я делаю это

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()
                    }
                }

            });

        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...