Java 如何使用ApachePOI从excel文件中读取值
我目前正在为我的计算机科学课做一个项目,我试图找出如何从excel中的文件中检索和打印某些值。例如,如何打印第6行第J列中的整数 更好的是,有没有办法返回第1列中字符串的行号?例如,如果在第1列中有一个字符串“Phone”,我可以使用命令返回“Phone”的第一个实例的行号吗Java 如何使用ApachePOI从excel文件中读取值,java,excel,apache-poi,Java,Excel,Apache Poi,我目前正在为我的计算机科学课做一个项目,我试图找出如何从excel中的文件中检索和打印某些值。例如,如何打印第6行第J列中的整数 更好的是,有没有办法返回第1列中字符串的行号?例如,如果在第1列中有一个字符串“Phone”,我可以使用命令返回“Phone”的第一个实例的行号吗 我看了其他的问题,没有一个能充分回答我自己的问题。这里你可以参考这个类文件来迭代excel文件 import java.io.File; import java.io.FileInputStream; import jav
我看了其他的问题,没有一个能充分回答我自己的问题。这里你可以参考这个类文件来迭代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();
}
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.UnsupportedEncodingException;
导入java.text.ParseException;
导入java.util.Iterator;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.DataFormatter;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公开课考试{
私有静态最终字符串文件_NAME=“/users/developer/Documents/myFile.xlsx”;
public void employeesUpload(){
字符串fName=“”;
字符串lName=“”;
字符串phoneNumber=“”;
字符串email=“”;
字符串性别=”;
字符串employeeCode=“”;
试一试{
FileInputStream excelFile=newfileinputstream(新文件(文件名));
工作簿=新XSSF工作簿(Excel文件);
工作表数据类型工作表=工作簿。getSheetAt(0);
迭代器迭代器=datatypeSheet.Iterator();
int rowIndex=0;
DataFormatter formatter=新的DataFormatter();
while(iterator.hasNext()){
Row currentRow=iterator.next();
如果(行索引>0){
迭代器cellIterator=currentRow.Iterator();
employeeCode=fName=lName=phoneNumber=email=gender=“”;
int cellIndex=0;
而(cellIndex用户)使用此jar
使用注释轻松阅读excel
public class ExcelImportNewDateTest {
@Test
public void importTest() {
ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
long start = new Date().getTime();
List<NewDateEntity> list = ExcelImportUtil.importExcel(
new File(FileUtilTest.getWebRootPath("import/ExcelNewDateTest.xlsx")), NewDateEntity.class, params);
System.out.println(new Date().getTime() - start);
Assert.assertEquals(list.size(), 100);
System.out.println(list.size());
System.out.println(ReflectionToStringBuilder.toString(list.get(1)));
}
公共类ExcelImportNewDateTest{
@试验
公共无效导入集(){
ImportParams参数=新的ImportParams();
参数设置标题(1);
参数设置头枕(1);
长启动=新日期().getTime();
List List=ExcelImportUtil.importExcel(
新文件(FileUtilTest.getWebRootPath(“import/ExcelNewDateTest.xlsx”)、NewDateEntity.class、params);
System.out.println(new Date().getTime()-start);
Assert.assertEquals(list.size(),100);
System.out.println(list.size());
System.out.println(reflectionStringBuilder.toString(list.get(1));
}
}正确的方法是阅读Apache POI用户指南,尝试教程,并运行一些实验。当您有一个程序,您认为应该得到正确的值,但没有得到正确的值时,您可以在StackOverflow上询问该程序的相关问题。如果您不具备pr的基本知识,那么答案将没有意义问题域。项目的基本要点是,您将能够学习一些新的东西。这可能需要一些时间,但值得投资。我建议您阅读Apache POI文档,其中也有示例()。如@realpoint所说,如果您有问题,请返回有错误的代码。学习愉快:)这回答了你的问题吗?