2013-07-21 3 views
1

Excel 파일을 읽으려고합니다. 파일은 내 작업 공간의 RoCom_DB 폴더에 보관되며 파일 이름은 RoCom.xlsx입니다. android에서 Excel 파일을 읽는 중 문제가 발생했습니다.

내가 다음 코드를 사용하여 파일을 읽으려고 오전 :

public String readComplexExcelFile(Context context){ 

     try{ 
      // Creating Input Stream 
      File file = new File(Environment.getExternalStorageDirectory() 
        + "/Android/data/" + getApplicationContext().getPackageName() 
        + "/RoCom_DB/", "RoCom.xlsx"); 

      FileInputStream myInput = new FileInputStream(file); 

      // Create a POIFSFileSystem object 
      POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); 

      // Create a workbook using the File System 
      HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); 

      // Get the first sheet from workbook 
      HSSFSheet mySheet = myWorkBook.getSheetAt(0); 

      /** We now need something to iterate through the cells.**/ 
      Iterator<Row> rowIter = mySheet.rowIterator(); 

      while(rowIter.hasNext()){ 
       HSSFRow myRow = (HSSFRow) rowIter.next(); 
       Iterator<Cell> cellIter = myRow.cellIterator(); 
       while(cellIter.hasNext()){ 
        HSSFCell myCell = (HSSFCell) cellIter.next(); 
        Log.d("", "Cell Value: " + myCell.toString()); 
        Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }catch (Exception e){ 
      e.printStackTrace(); 
      } 
     return ""; 
    } 

문제는 내가 파일을 읽을하려고 할 때마다, 나는이 File not found exception를 얻을. 나는이 하나 poi-3.7.jar 필요한 사용하고 코드의 조각을 유지 한 내 manifest.xml : 그것은 단지 지원하는 1메가바이트 개까지 엑셀 내 파일이 나는이 assets 디렉토리에서 엑셀을 사용하지 않으

<uses-permission 
     android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

크기가 커질 가능성이있다.

아무도 내가 뭘 잘못하고 있다고 말할 수 있습니까? 어떤 도움이라도 대단히 감사합니다. 감사 .

답변

1

나는 이것을 아주 이상한 방식으로 작동하도록했다. .

> 먼저 POI.jar을 제거하고 대신 jxl.jar을 사용했습니다. 그렇다면 다시 엑셀 워크 북은 xslx의 형식 이었기 때문에 2007 년 엑셀 2007에 있었기 때문에 xls 즉 Excel (97-2003) 형식으로 변환했습니다.

2

그럼 난 다음 명령을 사용하여 sdcard에 내 엑셀을 밀어> :

  • A> 우선 (Windows 용) runcmd

    을 제공 b>를 adb.exe가있는 곳으로 이동합니다. ( 안드로이드 -> SDK -> 플랫폼 도구)

    c> adb.exe가 저장된 폴더에 xls 파일을 복사하십시오.

    d 이제 adb 셸을 실행하십시오. 유닉스 쉘을 열 것입니다.CD/MNT와 SD 카드의 변경 허가 사용 : 이동 chmod 777 /sdcard

    전자> 지금 사용하여 배치 프롬프트로 돌아 : exit 명령 입력 : 이 adb push file.xls /mnt/sdcard/

    F> 다음 cd 변경을 사용하여 내부 /mnt/sdcard/ 이동합니다 사용하여 파일에 대한 권한을 는 : chmod 777 file.xls

3> 이제 모든 중요한 일들이 완료되어, 나는이 문제를 해결하기 위해 다음 코드 조각을 썼다 :

public String readComplexExcelFile(Context context, String userInput){ 
     String requiredContents = ""; 
     try{ 

      File inputWorkbook = new File(Environment.getExternalStorageDirectory()+"/myFile.xls"); 
      Workbook w; 

      // Create a workbook using the File System 
      w = Workbook.getWorkbook(inputWorkbook); 

      // Get the first sheet from workbook 
      Sheet sheet = w.getSheet(0); 

      /** We now need something to iterate through the cells.**/ 
      for (int j = 0; j < sheet.getColumns(); j++) { 
       for (int i = 0; i < sheet.getRows(); i++) { 
       Cell cell = sheet.getCell(j, i); 
       if(cell.getContents().equalsIgnoreCase(userInput)){ 
        Cell cellCorrespond = sheet.getCell(j+1, i); 
        requiredContents = cellCorrespond.getContents(); 
        break; 
       } 

       } 
      } 


     }catch (Exception e){ 
      e.printStackTrace(); 
      } 
     return requiredContents; 
    } 

같은 프로세스를 진행하는 동안 많은 행운을 겪지 않는 사람들에게 도움이되기를 바랍니다. 건배!

0

public File (String dirPath, String name)은 경로와 이름 사이에 경로 구분 기호를 넣으므로 'RoCom_DB'다음에있는 마지막 '/'를 제거해야합니다.

또한 문자열이 값

Environment.getExternalStorageDirectory() 
       + "/Android/data/" + getApplicationContext().getPackageName() 
       + "/RoCom_DB" 

를 저장하고 올바르게 형성되고 있는지 확인을 표시 할 수 있습니다.

+0

감사합니다. 나는 그것을 시도하고 다시 연락 할 것입니다. –

관련 문제