I am trying to read an excel file. The file is kept in my work space in a folder called RoCom_DB
and the file name is RoCom.xlsx
.
I am trying to read the file using the following code :
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 "";
}
The problem is every time i try to read the file, i get a File not found exception
. I have used the necessary poi-3.7.jar
for this one and kept this piece of code in my manifest.xml
:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I don't want to use the excel from assets
directory as it only supports excel upto 1 mb and my file has the potential to increase in size .
Can anybody tell me what i am doing wrong ? Any help is greatly appreciated . Thanks .
I got this one to work in quite a strange way. .
1> First of all i got rid of
POI.jar
and instead usedjxl.jar
. Then again my excel work book was in the format ofxslx
becuase it was in ms excel 2007, so i converted it toxls
i.e. excel(97-2003) format.2> Then i pushed my excel to sdcard using the following commands :
a> First give
cmd
inrun
(for windows)b> Navigate to where your
adb.exe
is present.(It will be inside android-->sdk-->platform tools)c> Then copy your xls to that folder where adb.exe is kept.
d> Now run adb shell. Will open a unix shell. Go to : cd /mnt and change permission of sd card using :
chmod 777 /sdcard
e> Now return to batch prompt using :
exit
command and type :adb push file.xls /mnt/sdcard/
f> Then go inside
/mnt/sdcard/
usingcd
and change permission for file using :chmod 777 file.xls
3> Now that all important things are done , i wrote the following piece of code to work this out :
Hopefully, this will help people who are stuck without much luck while going through the same process. Cheers !
public File (String dirPath, String name) places a path separator between the path and the name, so you should remove the last '/' after 'RoCom_DB'.
You might also store this value
in a string and display it to make sure it is correctly formed.