gpt4 book ai didi

java - 使用 Java Apache POI 搜索特定 Excel 单元格

转载 作者:行者123 更新时间:2023-12-01 18:52:50 25 4
gpt4 key购买 nike

我正在使用 Apache POI API 读取 Excel 文件并检查可用或不可用的产品数量。我使用下面的代码成功读取 Excel 文件,但无法读取特定单元格(数量列)来检查天气要求的产品是否可用

    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

最佳答案

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

});

}
}

关于java - 使用 Java Apache POI 搜索特定 Excel 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com