简体   繁体   中英

want to print a particular cell's all the row values in excel in java

After getting a paticular item name and price I store it in excel sheet. Now I compare their price and get lowest price, but how can i print that lowest price all detail

my code is

public static void getMinPhonePrice() throws Exception {

        File file = new File("C:\\Users\\user\\Desktop\\demo.xlsx"); 

         FileInputStream fs = new FileInputStream(file);

            XSSFWorkbook wb=new XSSFWorkbook(fs);
            String min = wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue();
            int value_min = Integer.parseInt(min.substring(1).replace(",", ""));

            String getText = null;
            XSSFSheet sh1= wb.getSheetAt(0);
            for(int j=0;j<3;j++) {
             getText =  sh1.getRow(0).getCell(j).getStringCellValue();
             System.out.println(getText);
            }



            for (int i = 1; i <=sh1.getLastRowNum(); i++) {

                String c =  sh1.getRow(i).getCell(1).getStringCellValue();

                int value = Integer.parseInt(c.substring(1).replace(",", ""));

                if(value < value_min) {
                    value_min=value;
                    for(int k=0;k<3;k++) {
                    getText = sh1.getRow(i).getCell(k).getStringCellValue();

                     System.out.println("minimum item detail"+getText);
                    }


            }

        }    

        }
}

but it not printing my minimum item detail

It seems like you don't need nested loop, and you need store other cell to string variable. Please try the bellow code.

    File file = new File("phone_compare.xlsx");
    FileInputStream fs = new FileInputStream(file);

    XSSFWorkbook wb=new XSSFWorkbook(fs);
    String min = wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue();
    int value_min = Integer.parseInt(min.substring(1).replace(",", ""));

    String getText = null;

    String str1 =  wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue();
    String str2 =  wb.getSheetAt(0).getRow(0).getCell(2).getStringCellValue();
    getText = str1 +" " +value_min +" " +str2;
    XSSFSheet sh1= wb.getSheetAt(0);


    for (int i = 0; i <=sh1.getLastRowNum(); i++) {

        String c =  wb.getSheetAt(0).getRow(i).getCell(1).getStringCellValue();

        int value = Integer.parseInt(c.substring(1).replace(",", ""));

        if(value < value_min) {
            value_min=value;
            str1 =  wb.getSheetAt(0).getRow(i).getCell(0).getStringCellValue();
            str2 =  wb.getSheetAt(0).getRow(i).getCell(2).getStringCellValue();
            getText = str1 +" " +value_min +" " +str2;



    }

}    
System.out.println(getText);
}

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