2014-11-05 63 views
0

我想上傳一個excel文件並閱讀它。當我給出一個絕妙的路徑時,我可以讀取該文件,但是當我上傳它時不想工作。我嘗試了不同的東西,但找不到解決方案。 我沒有使用JSP和servlet,但HTML /控制器/服務獲取上傳文件的目錄java

html頁面

<h:form enctype="multipart/form-data"> 
     <h:panelGrid> 
      <h:outputLabel value="Upload file: " style="font-weight:bold" /> 
      <h:inputFile value="#{uploadController.path}" /> 

      <h:commandButton value="Excel lezen en opslaan" action="# {uploadController.excelOpslaan}"/> 
     </h:panelGrid> 
</h:form> 

控制器

private String path; 


public String getPath() { 
    return path; 
} 

public void setPath(String path) { 
    this.path = path; 
} 

public void excelOpslaan(){ 

    PuntenService.getExcel(path); 
} 

服務

public List<String> getExcel(String path); 

服務IMPL

private List<String> list = new ArrayList<String>(); 

@Override 
public List<String> getExcel(String path){ 

    FileInputStream file = null; 
    OPCPackage pkg = null; 



    try { 

     file = new FileInputStream(path); 
     // create a new OPC Package to obtain a workbook 
     pkg = OPCPackage.open(file); 

     //Create Workbook instance holding reference to .xlsx file 
     XSSFWorkbook workbook = new XSSFWorkbook(pkg); 

     //Get first/desired sheet from the workbook 
     XSSFSheet sheet = workbook.getSheetAt(0); 

     //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 

      if (row.getRowNum() <= 5) { 
      continue;// skip to read the first 7 row of file 
      } 


      Iterator<Cell> cellIterator = row.cellIterator(); 

      while (cellIterator.hasNext()) { 
       Cell cell = cellIterator.next(); 

       switch (cell.getCellType()) 
       { 
        case Cell.CELL_TYPE_STRING: 
         list.add(cell.getStringCellValue()); 
         break; 
       } 

      } 
      //System.out.println(""); 
     } 
     file.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return list; 


} 

回答

0

我不會深入到您的程序中,但會嘗試將其調整爲java7 +標準(例如,存儲路徑爲路徑,而不是字符串),也許它會幫助一些..

一些例子

List<String> readSmallTextFile(String aFileName) throws IOException { 
    Path path = Paths.get(aFileName); 
    return Files.readAllLines(path, ENCODING); 
    } 

    //For larger files 

    void readLargerTextFile(String aFileName) throws IOException { 
    Path path = Paths.get(aFileName); 
    try (Scanner scanner = new Scanner(path, ENCODING.name())){ 
     while (scanner.hasNextLine()){ 
     //process each line in some way 
     log(scanner.nextLine()); 
     }  
    } 
    } 

    void readLargerTextFileAlternate(String aFileName) throws IOException { 
    Path path = Paths.get(aFileName); 
    try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)){ 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
     //process each line in some way 
     log(line); 
     }  
    } 
    } 

http://www.javapractices.com/topic/TopicAction.do?Id=42