2013-05-03 58 views
0

代碼:使用poi解析excel文件並在控制檯中輸出輸出,並創建一個新的excel文件以顯示輸出。如何在dto中存儲解析的.xslx文件

XSSFWorkbook workbook = new XSSFWorkbook(fileName); 
XSSFSheet sheet = workbook.getSheetAt(0); 
    XSSFRow row ; 
    XSSFCell cell; 


    Iterator<Row> rows = sheet.rowIterator(); 

    while(rows.hasNext()) 
    { 
     row = (XSSFRow)rows.next(); 
     Iterator<Cell> cells = row.cellIterator(); 
     while(cells.hasNext()) 
     { 
      cell = (XSSFCell)cells.next(); 

      switch(cell.getCellType()) 
      { 
      case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t"); 
      break; 
      case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t"); 
      break; 
      case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t"); 
      break; 
      } 

     }System.out.println(""); 
    }fileName.close(); 

    FileOutputStream out = new FileOutputStream(new File("C://data.xlsx")); 
    workbook.write(out); 
    out.close(); 

輸出: 標識
名稱
位置
角色
工資

111.0
庫馬爾

開發
1000.0

112.0
拉森
班加羅爾
開發
2000.0

查詢: 1.如何獲得相同的格式輸出在Excel? 2.如何將輸出存儲在DTO對象中?

回答

0

嘗試這種方式來增加價值DTO

具有類似性質和setter和getter與創建一個學生DTO Id,Name,Location,Role,Salary

 while(rows.hasNext()) 
      { 
       row = (XSSFRow)rows.next(); 
       Iterator<Cell> cells = row.cellIterator(); 
    StudentDTO std = new StudentDTO(); 
       while(cells.hasNext()) 
       { 
        cell = (XSSFCell)cells.next(); 

        switch(cell.getCellType()) 
        { 
        case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()+"\t\t"); 
    std.setId(cell.getBooleanCellValue()); 
        break; 
        case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()+ "\t\t"); 
    std.setName(cell.getNumericCellValue()); 
        break; 
        case Cell.CELL_TYPE_STRING:System.out.println(cell.getStringCellValue()+ "\t\t"); 
std.setLocation(cell.getStringCellValue()); 
        break; 
        } 

       }System.out.println(""); 
      }