获得特定商品名称和价格后,我将其存储在 Excel 工作表中。现在我比较他们的价格并获得最低价格,但是我如何打印最低价格的所有详细信息
我的代码是
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
最佳答案
看来您不需要嵌套循环,并且需要将其他单元格存储到字符串变量。请尝试以下代码。
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);
}
关于java - 想要在java中打印excel中特定单元格的所有行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57288552/