我有一个excel文件,其中有一些列,如Firstname,LastName,ID等.... 我想获取每个给定列的所有数据并将它们存储在对象的arraylist中。问题是,我无法确保我获得的数据属于一个明确的列名,如Firstname列或LastName或其他列。例如,现在我有一个excel文件,其序列为 (LastName,Firstname,ID) ,第二个excel文件具有不同的序列 (ID,姓氏,名字) 即可。 如何确保此姓氏用于此名字和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学生班
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");
}
}
答案 0 :(得分:1)
在您的情况下,您阅读的前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 ...
}
}