2017-09-07 180 views
0

我正在處理一個程序,其中來自excel表格的行數據應該顯示爲輸出。一個id被用作輸入,它將映射到顯示輸出的特定行。我在java上使用apache poi來編寫代碼。我想顯示輸出,而不使用我目前在我的編程中使用的單元格。有沒有直接打印行對象的行數據的函數?如何在apache poi中不使用公式單元格獲取行數據

package exceldata; 
import org.apache.poi.ss.usermodel.*; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.*; 

public class exceldata { 

private static final String FILE_NAME = "C:\\Users\\agandiko\\Desktop\\data.xlsx"; 

    public static void main(String[] args) { 

     try {System.out.println("Enter the id:"); 
      Scanner s=new Scanner(System.in); 
      String field=s.next(); 
      int on=0; 
      int n=0; 
      int cc=1; 
      FileInputStream excelFile = new FileInputStream(new File(FILE_NAME)); 
      Workbook wb = new XSSFWorkbook(excelFile); 
      DataFormatter formatter = new DataFormatter(); 
      for (int i = 0; i < wb.getNumberOfSheets(); i++) { 
       System.out.println(); 
       System.out.println(); 
       System.out.println("Data for Sheet"+" "+(i+1)); 
       System.out.println("-----------------------------------"); 
       System.out.println(); 
       System.out.println(); 
      Sheet datatypeSheet = wb.getSheetAt(i); 
      Iterator<Row> iterator = datatypeSheet.iterator(); 
      int noOfColumns = datatypeSheet.getRow(0).getPhysicalNumberOfCells(); 
      while (iterator.hasNext()) { 

       Row currentRow = iterator.next(); 
       Iterator<Cell> cellIterator = currentRow.iterator(); 

       while (cellIterator.hasNext()) { 

        Cell currentCell = cellIterator.next(); 
        Cell k=currentCell; 
        //getCellTypeEnum shown as deprecated for version 3.15 
        //getCellTypeEnum ill be renamed to getCellType starting from version 4.0 
        String val=formatter.formatCellValue(currentCell).toString(); 

        if((val.equals(field))) 
        { 
         while(cellIterator.hasNext()){ 
        if(cc==1){System.out.print(formatter.formatCellValue(k).toString()+" ");} 
        else{currentCell = cellIterator.next();System.out.print(formatter.formatCellValue(currentCell).toString()+" ");} 

         cc++; 
         } 

         // System.out.print(currentRow.toString()+" "); 
         // currentRow = iterator.next(); 
         // cellIterator = currentRow.iterator(); 
         // on=1;n++;} 

          //System.out.println(cc); 
       } 

       // if(n==noOfColumns){System.out.println();n=0;} 

       //on=0; 


      } 

     cc=1; } 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

}

+1

這是我不清楚你想要打印行數據(到System.out)? – JensS

+0

是的,我想打印它在System.out –

+1

所以你的意思是currentRow.toString()的「格式化版本」。據我所知,這是目前不可能的,最好的辦法就是你做到這一點(=遍歷單元格)。 – JensS

回答

0

這就是我所理解的用例:找一些ID(代碼假定它是每個表中的第一列)並打印整行,如果它的ID匹配進入;這裏是如何可以解決(取決於您的工作表中的數據,一些空的檢查可能需要增加,但總體上它的工作原理「原樣」):

public static void main(String[] args) throws IOException { 

     System.out.println("Enter the id:"); 
     Scanner s = new Scanner(System.in); 
     String id = s.next(); 

     FileInputStream excelFile = new FileInputStream(new File(FILE_NAME)); 
     Workbook wb = new XSSFWorkbook(excelFile); 
     DataFormatter formatter = new DataFormatter(); 
     for (int i = 0; i < wb.getNumberOfSheets(); i++) { 
      System.out.println(); 
      System.out.println(); 
      System.out.println("Data for Sheet" + " " + (i + 1)); 
      System.out.println("-----------------------------------"); 
      System.out.println(); 
      System.out.println(); 
      Sheet datatypeSheet = wb.getSheetAt(i); 
      Iterator<Row> iterator = datatypeSheet.iterator(); 

      while (iterator.hasNext()) { 
       Row currentRow = iterator.next(); 
       if(id.equals(formatter.formatCellValue(currentRow.getCell(0)))) { 
        currentRow.cellIterator().forEachRemaining(c -> System.out.print(formatter.formatCellValue(c) + " ")); 
       } 
      } 
     } 
     wb.close(); 
     s.close(); 
} 
+0

謝謝!你的代碼是正確的 –

+0

非常好。那麼請你接受關閉這個話題的答案。 – JensS

相關問題