gpt4 book ai didi

java - 如何使用 Apache POI 从 Excel 文件读取值

转载 作者:行者123 更新时间:2023-12-01 19:56:36 24 4
gpt4 key购买 nike

我目前正在为我的计算机科学类(class)开发一个项目,我试图弄清楚如何从 Excel 中的文件中检索和打印某些值。例如,我如何打印 J 列第 6 行中的整数?

更好的是,有没有办法让我返回第 1 列中字符串的行号?例如,如果我在第 1 列中有一个字符串“Phone”,我可以使用命令返回“phone”的第一个实例的行号吗?

我查看了其他问题,但没有一个充分回答了我自己的问题。

最佳答案

在这里,您可以引用此类文件来迭代 Excel 文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {

private static final String FILE_NAME = "/users/developer/Documents/myFile.xlsx";

public void employeesUpload() {
String fName = "";
String lName = "";
String phoneNumber = "";
String email = "";
String gender = "";
String employeeCode = "";

try {

FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();

int rowIndex = 0;
DataFormatter formatter = new DataFormatter();
while (iterator.hasNext()) {
Row currentRow = iterator.next();
if (rowIndex > 0) {
Iterator<Cell> cellIterator = currentRow.iterator();

employeeCode = fName = lName = phoneNumber = email = gender = "";

int cellIndex = 0;
while (cellIndex <= 5) {

Cell currentCell = currentRow.getCell(cellIndex);
if (cellIndex == 4) {
employeeCode = formatter.formatCellValue(currentCell).trim();
}

if (cellIndex == 1) {
fName = formatter.formatCellValue(currentCell).trim();
}

if (cellIndex == 2) {
lName = formatter.formatCellValue(currentCell).trim();
}

if (cellIndex == 0) {
email = formatter.formatCellValue(currentCell);
email = email.trim().toLowerCase();
}

if (cellIndex == 3) {
phoneNumber = formatter.formatCellValue(currentCell).trim();
}

cellIndex++;
}
Cell resultCell = currentRow.getCell(7);

if (resultCell == null) {
resultCell = currentRow.createCell(7);
}

Cell employementIdCell = currentRow.getCell(8);
if (employementIdCell == null) {
employementIdCell = currentRow.createCell(8);
}

if (true) {
resultCell.setCellType(Cell.CELL_TYPE_STRING);
employementIdCell.setCellValue("Success");
resultCell.setCellValue(email);

} else {
resultCell.setCellType(Cell.CELL_TYPE_STRING);
resultCell.setCellValue("Error");
}

}
rowIndex++;
}

FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
workbook.write(outputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

public static void main(String[] args) throws ParseException, UnsupportedEncodingException {

Test employeesBulkUpload = new Test();
employeesBulkUpload.employeesUpload();

}
}

Hope this helps :)

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

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