2016-07-22 86 views
0

我想從使用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; 
    } 

enter image description here

我要在ArrayList中在單一時間

List.add(new Data(1,"Amit","shukla")); 

增加該小區的數據是這樣,但數據的迭代器返回的是一個一個像第一個輸出 th恩阿米特然後舒克拉這是真的很難添加到ArrayList中

我試過這麼多的數據在一個單一的行添加到ArrayList中,但我不能。如果你幫我解決這個問題,這將會非常有幫助。

+0

難道你不能只是添加setter到你的數據類,並相應地建立它時循環遍歷所有單元格中的行,然後當行完成後將它添加到arrayList? – Zeromus

回答

1

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() + "\t"); 
          i=Integer.parseInt(cell.getNumericCellValue()); 
          Data d= new Data(); 
          d.setId(cell.getNumericCellvalue()); 


         break; 
        case Cell.CELL_TYPE_STRING: 
         System.out.print(cell.getStringCellValue() + "\t"); 
         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(); 
    } 
+0

創建一個HashMap,它將保存id和數據對象,稍後將值轉換爲List,dataList將具有所需的對象列表 – user2025528

相關問題