2016-06-22 21 views
-1

我正在嘗試從.xlsx excel版本中檢索用戶憑據。我已經編寫了用於從Excel中檢索數據的代碼,但無法將日期插入到應用程序中。請幫助我。想使用硒從excel中讀取用戶憑證

FileInputStream stream=new FileInputStream("D:/Ravi-Training/ExcelRead.xlsx"); 
    XSSFWorkbook wb=new XSSFWorkbook(stream); 
    XSSFSheet sheet=wb.getSheetAt(0); 
    /*Iterator<Row> rt=sheet.iterator(); 
    while(rt.hasNext()) 
    { 
     Row row=rt.next(); 
     Iterator<Cell> ct=row.cellIterator(); 
     while(ct.hasNext()) 
     { 
      Cell cell=ct.next(); 
     }*/ 
    System.out.println(sheet.getSheetName()); 
    System.out.println(sheet.getLastRowNum()); 
    XSSFRow row=sheet.getRow(1); 
    XSSFCell cell1=row.getCell(0); 
    XSSFCell cell2=row.getCell(1); 
    XSSFCell cell3=row.getCell(2); 
    System.out.println(cell1); 
    System.out.println(cell2); 
    System.out.println(cell3); 
+0

你的意思是無法插入..你的意思是你將無法從Excel中獲取數據。 – selva

+0

你需要提供更多的細節,你已經嘗試過什麼,結果是什麼。目前還不清楚你在問什麼。你是否證實你可以從Excel讀取數據?你有沒有嘗試在應用程序中插入硬編碼的字符串?徹底。 – JeffC

回答

0
FileInputStream fs=new FileInputStream("Path of excel sheet"); 
    Workbook wb1=WorkbookFactory.create(fs); 
    Sheet sh=wb1.getSheetAt(0); 
    Row row=sh.createRow(8); 
    //Write data 
    Cell cell=row.createCell(8); 
    cell.setCellType(Cell.CELL_TYPE_STRING); 
    cell.setCellValue("ravi"); 
    FileOutputStream fos=new FileOutputStream("Path of excel sheet"); 
    wb1.write(fos); 
    fos.close(); 
0

請嘗試以下代碼。

 FileInputStream inputStream = new FileInputStream(new File(filepath)); 
     XSSFWorkbook wb = new XSSFWorkbook(inputStream); 
     XSSFSheet sheet = null; 

     if (sheetobj instanceof Integer) { 
      sheet = wb.getSheetAt((int) sheetobj); 
     } 
     if (sheetobj instanceof String) { 
      sheet = wb.getSheet((String) sheetobj); 
     } 

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

     Row row = rowIt.next(); 

     int noOfrow = sheet.getLastRowNum(); 
     int noOfcolumn = row.getLastCellNum(); 

     excelValue = new String[noOfrow][noOfcolumn]; 

     System.out.println("No: of column " + noOfcolumn + " no: of row " + noOfrow); 

     wb.close(); 

希望這會有所幫助。