使用POI Apache从Excel读取数据时向ArrayList添加数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用POI Apache从Excel读取数据时向ArrayList添加数据相关的知识,希望对你有一定的参考价值。
我正在尝试使用POI Apache从Excel工作表中读取数据。我遇到的问题是我想同时读取行中所有单元格的数据并将其存储在Type Class的ArrayList中,但输出只是逐个单元格。
这是打开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() + " ");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + " ");
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"));
但迭代器返回的数据是逐个输出1然后输出amit然后shukla,这真的很难添加到arraylist
我尝试过将数据添加到ArrayList的单行,但我做不到。如果你的家伙帮我解决这个问题,那将会非常有帮助。
答案
this.path = path;
try
{
FileInputStream file = new FileInputStreaHashMap<K, V>ile(path));
HashMap<Integer, Data> mp= new HashMap<Integer, Data>();
//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
int i=0;
int j=0;
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + " ");
i=Integer.parseInt(cell.getNumericCellValue());
Data d= new Data();
d.setId(cell.getNumericCellvalue());
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + " ");
if( j==0){
Data data= mp.get(i);
data.setName(cell.getStringCellValue());
mp.put(i, data);
j=j+1;
}
else
{
Data data= mp.get(i);
data.setLastName(cell.getStringCellValue());
mp.put(i, data);
j=0;
}
break;
}
}
System.out.println("");
}
List<Data> dataList= new ArrayList<Data>();
for (Data d : mp.values()) {
dataList.add(d);
}
file.close();
}
catch (Exception e)
{
e.printStackTrace();
}
另一答案
您可以使用此方法通过单次迭代添加Excel的单行,
public void ReadExcel(String filePath,String fileName,String sheetName) throws InterruptedException, IOException{
File file = new File(filePath+""+fileName);
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
Workbook AddCatalog = null;
//Find the file extension by splitting file name in substring and getting only extension name
String fileExtensionName = fileName.substring(fileName.indexOf("."));
//Check condition if the file is a .xls file or .xlsx file
if(fileExtensionName.equals(".xls")){
//If it is .xls file then create object of HSSFWorkbook class
AddCatalog = new HSSFWorkbook(inputStream);
}
else if(fileExtensionName.equals(".xlsx")){
//If it is .xlsx file then create object of XSSFWorkbook class
AddCatalog = new XSSFWorkbook(inputStream);
}
//Read sheet inside the workbook by its name
Sheet AddCatalogSheet = AddCatalog.getSheet(sheetName);
//Find number of rows in excel file
int rowcount = AddCatalogSheet.getLastRowNum()- AddCatalogSheet.getFirstRowNum();
System.out.println("Total row number: "+rowcount);
for(int i=1; i<rowcount+1; i++){
//Create a loop to get the cell values of a row for one iteration
Row row = AddCatalogSheet.getRow(i);
List<String> arrName = new ArrayList<String>();
for(int j=0; j<row.getLastCellNum(); j++){
// Create an object reference of 'Cell' class
Cell cell = row.getCell(j);
// Add all the cell values of a particular row
arrName.add(cell.getStringCellValue());
}
System.out.println(arrName);
System.out.println("Size of the arrayList: "+arrName.size());
// Create an iterator to iterate through the arrayList- 'arrName'
Iterator<String> itr = arrName.iterator();
while(itr.hasNext()){
System.out.println("arrayList values: "+itr.next());
}
}
}
另一答案
您在Switch和Case中遇到问题部件更改应该可以正常工作:
switch (cell.getCellType()) {
case NUMERIC:
System.out.print(cell.getNumericCellValue() + " ");
break;
case STRING:
System.out.print(cell.getStringCellValue() + " ");
break;
}
以上是关于使用POI Apache从Excel读取数据时向ArrayList添加数据的主要内容,如果未能解决你的问题,请参考以下文章
在使用 apache poi 从扩展名为 xlsx 的 Excel 文件中读取数据时,需要很长时间
使用 Apache POI 从 Java 中的 Excel 中读取公式字段
无法从Excel读取'java.lang.ClassCastException:org.apache.poi.hssf.usermodel.HSSFCell无法强制转换为java.lang.S