我想从使用POI Apache的Excel工作表中读取数据。我遇到的问题是我想同时读取一行的所有单元格的数据,并将它存储在类型类的ArrayList中,但输出只是逐个单元格。使用POI从Excel中读取数据时将数据添加到ArrayList Apache
这里是打开Excel工作表并逐个读取数据单元的类。
package testing;
import javax.swing.JFileChooser;
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();
//Check the cell type and format accordingly
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();
}
}
}
数据类
package testing;
public class Data {
int ID;
String F_Name,L_Name;
public Data(int ID, String F_Name, String L_Name) {
this.ID = ID;
this.F_Name = F_Name;
this.L_Name = L_Name;
}
public int getID() {
return ID;
}
public String getF_Name() {
return F_Name;
}
public String getL_Name() {
return L_Name;
}
我要在ArrayList中在单一时间
List.add(new Data(1,"Amit","shukla"));
增加该小区的数据是这样,但数据的迭代器返回的是一个一个像第一个输出 th恩阿米特然后舒克拉这是真的很难添加到ArrayList中
我试过这么多的数据在一个单一的行添加到ArrayList中,但我不能。如果你帮我解决这个问题,这将会非常有帮助。
难道你不能只是添加setter到你的数据类,并相应地建立它时循环遍历所有单元格中的行,然后当行完成后将它添加到arrayList? – Zeromus