gpt4 book ai didi

java - 如何使用 Apache POI 读取 MS Excel 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 11:31:28 25 4
gpt4 key购买 nike

我有一个 MS-Excel 文件,我想通过 Apache POI 读取该文件。我以前没有从事过这个工作,所以我不知道该怎么做。谁能帮我解决这个问题吗?

最佳答案

public List<Record> readBooksFromExcelFile(String excelFilePath)
throws IOException {

List<Record> listBooks = new ArrayList<Record>();
FileInputStream inputStream = new FileInputStream(new File(
excelFilePath));

Workbook workbook = new XSSFWorkbook(inputStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();

FormulaEvaluator objFormulaEvaluator = new XSSFFormulaEvaluator(
(XSSFWorkbook) workbook);
DataFormatter objDefaultFormat = new DataFormatter();
String cellValueStr;

int headingRow = 0;

System.out
.println("================== Start Reading Records From Excel ==================");
LogFileWriter
.appendToLogfile(
"================== Start Reading Records From Excel ==================",
true);

while (iterator.hasNext()) {

Row nextRow = iterator.next();
// to ignore the first row.

if (headingRow == 0) {
headingRow++;
continue;
}
Iterator<Cell> cellIterator = nextRow.cellIterator();
Record aRecord = new Record();

while (cellIterator.hasNext()) {
Cell nextCell = cellIterator.next();
nextCell.setCellType(Cell.CELL_TYPE_STRING);
int columnIndex = nextCell.getColumnIndex();

// to ignore the heading row in file.

switch (columnIndex) {
case 0:
// objFormulaEvaluator.evaluate(nextCell);
// cellValueStr =
// objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);

cellValueStr = (String) getCellValue(nextCell);
cellValueStr = cellValueStr.replace(" ", "");
aRecord.setIccid(cellValueStr);

// aRecord.setIccid((String) getCellValue(nextCell));
break;
case 1:
// objFormulaEvaluator.evaluate(nextCell);
// cellValueStr =
// objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);
// cellValueStr=cellValueStr.replace(" ", "");

cellValueStr = (String) getCellValue(nextCell);
cellValueStr = cellValueStr.replace(" ", "");
aRecord.setApn(cellValueStr);
break;
case 2:
// objFormulaEvaluator.evaluate(nextCell);
// cellValueStr =
// objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);
// cellValueStr=cellValueStr.replace(" ", "");

cellValueStr = (String) getCellValue(nextCell);
cellValueStr = cellValueStr.replace(" ", "");
aRecord.setIpAddress(cellValueStr);
break;
case 3:
// objFormulaEvaluator.evaluate(nextCell);
// cellValueStr =
// objDefaultFormat.formatCellValue(nextCell,objFormulaEvaluator);
// cellValueStr=cellValueStr.replace(" ", "");

cellValueStr = (String) getCellValue(nextCell);
cellValueStr = cellValueStr.replace(" ", "");
aRecord.setPdp(cellValueStr);
break;
}
}

System.out.println("READ: " + aRecord.toString());
LogFileWriter.appendToLogfile("READ: " + aRecord.toString(), true);
listBooks.add(aRecord);
}
// workbook.close();
System.out.println("================== Finished Reading Records From Excel ==================");
LogFileWriter.appendToLogfile("================== Finished Reading Records From Excel ==================",true);
return listBooks;
}

private Object getCellValue(Cell cell) {

switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();

case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();

case Cell.CELL_TYPE_NUMERIC:
return cell.getNumericCellValue();
}

return null;
}

这是 Record.java

public class Record {

private String iccid;
private String apn;
private String ipAddress;
private String pdp;
public String getIccid() {
return iccid;
}
public void setIccid(String iccid) {
this.iccid = iccid;
}
public String getApn() {
return apn;
}
public void setApn(String apn) {
this.apn = apn;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getPdp() {
return pdp;
}
public void setPdp(String pdp) {
this.pdp = pdp;
}
public String toString() {
return "Record [iccid=" + iccid + ", apn=" + apn + ", ipAddress="
+ ipAddress + ", pdp=" + pdp + "]";
}

}

关于java - 如何使用 Apache POI 读取 MS Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730829/

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