我想将数据传送到List
并显示它。但是输出显示4次。
我的excel
文件包含4
个数据。
我想得到一张唱片。我的代码是
public static List readDataFromExcel() throws IOException{
String filename = "path";
List sheetData = new ArrayList();
FileInputStream fis = null;
try {
fis = new FileInputStream(filename);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
XSSFRow row = (XSSFRow) rows.next();
Iterator cells = row.cellIterator();
List data = new ArrayList();
while (cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
String value=" ";
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
value = BigDecimal.valueOf(cell.getNumericCellValue()).toPlainString();
data.add(value);
break;
case Cell.CELL_TYPE_STRING:
value=cell.getStringCellValue();
data.add(value);
break;
case Cell.CELL_TYPE_BLANK:
value = " ".toString();
data.add(value);
break;
case Cell.CELL_TYPE_BOOLEAN:
value = Boolean.valueOf(cell.getBooleanCellValue()).toString();
data.add(value);
break;
}
sheetData.add(data);
}
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return sheetData;
}
主要方法
public static void main(String[] args) throws IOException {
List serverdetailsList = ReadDataFromExcel.readDataFromExcel();
List oneserverdetailsList = new ArrayList();
for (int i = 0; i < serverdetailsList.size(); i++) {
System.out.println(serverdetailsList.get(i));
}
}
输出图像
Excel ScrenShot
答案 0 :(得分:1)
我看到你将内部ArrayList(数据)添加到主arrayList(sheetdata)中,与你找到一个单元格的次数一样多。
方法应该是
while (rows.hasNext()) {
XSSFRow row = (XSSFRow) rows.next();
Iterator cells = row.cellIterator();
List data = new ArrayList();
while (cells.hasNext()) {
XSSFCell cell = (XSSFCell) cells.next();
String value=" ";
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
value = BigDecimal.valueOf(cell.getNumericCellValue()).toPlainString();
data.add(value);
break;
case Cell.CELL_TYPE_STRING:
value=cell.getStringCellValue();
data.add(value);
break;
case Cell.CELL_TYPE_BLANK:
value = " ".toString();
data.add(value);
break;
case Cell.CELL_TYPE_BOOLEAN:
value = Boolean.valueOf(cell.getBooleanCellValue()).toString();
data.add(value);
break;
}
//sheetData.add(data);
}
sheetData.add(data);
fis.close();
}
答案 1 :(得分:0)
每个循环可以使用2
for(ArrayList innerList :serverdetailsList)
{for(Object cellData:innerList )
{
if(cellData.toString().equalsIgnoreCase("OS1")){
//Your operation
}
}
}
希望这就是你要找的东西!!