java - 如何使用 Apache POI 从 excel 文件中读取值
问题描述
我目前正在为我的计算机科学课做一个项目,我试图弄清楚如何从 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();
}
}
希望这可以帮助 :)
推荐阅读
- laravel - 使用“with”加入 laravel 并在连接表中设置 where 语句
- javascript - 有没有办法在休息参数的输出中保留空格?
- swift - 有什么方法可以访问 WKInterfaceButton 上的 pressesbegan 方法
- android - 如果通知消息中有 URL,则在 Android 应用程序中打开来自 FCM 的通知消息作为链接/URL
- regex - 如何在非连续大写字母之前在字符串中添加空格,同时排除连续大写字母?
- c++ - 带有可选参数或重载的函数模板
- c++ - 考虑 CPU 拓扑与 OpenMP 并行化
- r - 如何使用 for 循环和 if 语句
- node.js - 如何在生产中崩溃时自动重启 Node 脚本?
- python - 如何使用正则表达式从通话记录中提取(说话者、文本)元组?