2014-03-05 19 views
0

我想從Excel文件中讀取特定列數據並在具有附加數據的文本文件中搜索列的相關標題。我想用Java poi將這些細節打印回另一個excel文件。 我的數據是這樣的:從Excel中讀取數據並使用Java POI從文本文件中檢索相關數據並在另一個excel中顯示結果

Excel中:

Name Number 
ABC 2345 
XYZ 6789 

文本文件:

Name|Class|School 
ABC|1|bnm 
XYZ|2|hjk 
KOP|3|ert 

我要在Excel輸出:

Name Class School 
ABC 1  bnm 
XYZ 2  hjk 





public static void main(String[] args) throws Exception { 
    String filename = System.getProperty("D:/Files/Input.xslx"); 
     List sheetData = new ArrayList(); 
     String columnWanted = "Name"; 
     Integer columnNo = null; 
     FileInputStream fis = null; 
     try { 
      fis = new FileInputStream(filename); 
      XSSFWorkbook workbook = new XSSFWorkbook(fis); 
      XSSFSheet sheet = workbook.getSheetAt(0); 
      Row firstRow = sheet.getRow(0); 
      for(Cell cell:firstRow){ 
       if (cell.getStringCellValue().equals(columnWanted)){ 
        columnNo = cell.getColumnIndex(); 
       } 
      } 
      if (columnNo != null){ 
      for (Row row : sheet) { 
       Cell c = row.getCell(columnNo); 
       if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) { 
        // Nothing in the cell in this row, skip it 
       } else { 
        sheetData.add(c); 
           } 
       }System.out.println(sheetData); 
      }else{ 
       System.out.println("could not find column "); 
      } 
    // Taking a new array list 'arr' for printing result data 
      List arr =new ArrayList(); 
      File f=new File("D:/Files/Details.txt"); 
      Scanner in=new Scanner(f); 
      System.out.println("Read Data From The Txt file "); 
      while(in.hasNext()) 
      {  
    arr.add(in.nextLine()); 
         } 
      System.out.println(arr); 
      } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (fis != null) { 
       fis.close(); 
      } 
     } 
    } 
     } 



I want help in searching Names in text file taking input from Input.xslx 
+0

我試圖檢索excel數據並將其保存到array.not能夠鏈接該數據並在文本文件中搜索 – user3382164

+0

我的意思是:顯示我們的代碼.. –

+0

我的答案中是否有足夠的信息?你需要進一步的指導嗎? –

回答

0

一般的方法我會採取將是:

  1. 拆分文本文件的行:String[] textRow = in.nextLine().split("\\|");
  2. 如果sheetData.contains(textRow[0])是真的,那麼創建一個使用POI三個新的細胞。

另外,我會在同一個工作簿中創建一個新的工作表來編寫新的單元格。如果需要,可以稍後移動單元格。這意味着,在測試過程中,您可以在每次實驗後刪除新的工作表,並且您的「源」工作表不會被修改。

相關問題