如何从Excel工作表中获取具体的列数据以将其存储在使用POI的对象的数组列表中?

我有一个像Firstname,LastName,ID等一些列的Excel文件…。 我想获取每个给定列的所有数据,并将它们存储在一个对象的数组列表中。 事情是,我不能确保我所得到的数据属于像名字列或姓氏或其他列的sepicific列名称。 例如,现在我有一个具有此序列(姓,名,ID)的Excel文件和具有不同序列(ID,姓氏,名字)的第二个Excel文件。 我如何确保这个姓氏是用于这个名字和ID? 我已经做了一些研究,但是我根本无法得到一些相似的东西。 如果有人能帮助我,我将不胜感激。

import javax.swing.JFileChooser; import javax.swing.JOptionPane; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.poi.ss.usermodel.Cell; 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.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcelDemo { ArrayList<Data> list = new ArrayList<>(); String path; public ReadExcelDemo(String path) { this.path = path; try { FileInputStream file = new FileInputStream(new File(path)); //Create Workbook instance holding reference to .xlsx file XSSFWorkbook workbook = new XSSFWorkbook(file); //Get first/desired sheet from the workbook XSSFSheet sheet = workbook.getSheetAt(0); System.out.println(""); //Iterate through each rows one by one Iterator<Row> rowIterator = sheet.iterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); //For each row, iterate through all the columns Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); cell.setCellType(Cell.CELL_TYPE_STRING); //Check the cell type and format accordingly JOptionPane.showMessageDialog(null,cell.getAddress()); switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue() + "\t"); break; case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + "\t"); break; } } System.out.println(""); } file.close(); } catch (Exception e) { e.printStackTrace(); } } } 

这是我的Arraylist学生class

 public class Student { private String Firstname; private String LastName; private String Username; public Student(String firstname, String lastName, String username) { this.Firstname = firstname; this.LastName = lastName; this.Username = username; } public String getFirstname() { return Firstname; } public void setFirstname(String firstname) { Firstname = firstname; } public String getLastName() { return LastName; } public void setLastName(String lastName) { LastName = lastName; } public String getUsername() { return Username; } public void setUsername(String username) { Username = username; } } 

这是我的主要课程

 import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { ReadExcelDemo re = new ReadExcelDemo( "E:/St.xlsx"); } } 

这是我目前的代码的结果

这是我的excel文件的一个例子

在你的情况下,你读到的前3个单元格名称

你可以使用row.getCell(columnIndex)看看这个代码。 它是逐列获取数据:

 for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) { row = sheet.getRow(rowIndex); if (row != null) { String cellValueMay = null; for (int colIndex = 0; colIndex < colCount; colIndex++) { if (colIndex == theColIndexYouWant) { cell = row.getCell(colIndex); if (cell != null) { // Found column and there is value in the cell. cellValueMaybeNull = cell.getStringCellValue(); break; } } // Do something with the cellValueMaybeNull here ... } }