问题在阅读中的Android Excel文件 [英] Issue while reading an excel file in android
问题描述
我想读的Excel文件。该文件保存在我的工作空间 RoCom_DB
文件名是 RoCom.xlsx
称为文件夹和
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
.
我想用下面的code来读取文件:
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 "";
}
问题是每次我尝试读取文件时,我收到了文件未发现异常
。我已经使用了必要的 POI-3.7.jar
为这一个,并保持这片code在我的的manifest.xml
:
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"/>
我不希望从资产使用Excel
目录,因为它只支持Excel高达1 MB和我的文件有扩大规模的潜力。
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 .
谁能告诉我什么,我做错了什么?任何帮助是极大的AP preciated。谢谢你。
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>所有我摆脱 POI.jar
和代替的第一 jxl.jar
。再说我的Excel工作簿是 XSLX
的格式,原因是其在MS Excel 2007中,所以我把它转换为 XLS
即Excel文件(97-2003)格式。
1> First of all i got rid of POI.jar
and instead used jxl.jar
. Then again my excel work book was in the format of xslx
becuase it was in ms excel 2007, so i converted it to xls
i.e. excel(97-2003) format.
2>然后,我把我的Excel中使用下列命令到SD卡:
2> Then i pushed my excel to sdcard using the following commands :
-
A>先给
CMD
在运行
(适用于Windows)
a> First give
cmd
inrun
(for windows)
B>导航到你的 adb.exe
是present。(这将是内部
机器人 - > SDK - >平台工具)
b> Navigate to where your adb.exe
is present.(It will be inside
android-->sdk-->platform tools)
C>那么你的XLS复制到adb.exe保持该文件夹。
c> Then copy your xls to that folder where adb.exe is kept.
D>现在运行的adb外壳。将打开一个UNIX外壳。转到:CD / mnt目录并且
使用SD卡的变更许可:搭配chmod 777 / SD卡
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>现在用回批提示:退出
命令,然后输入:亚行推file.xls到/ mnt / SD卡/
e> Now return to batch prompt using : exit
command and type :
adb push file.xls /mnt/sdcard/
f>组合利用内幕的/ mnt / SD卡/
CD
并变更许可
使用文件:搭配chmod 777 file.xls
f> Then go inside /mnt/sdcard/
using cd
and change permission
for file using : chmod 777 file.xls
3>现在所有重要的事情都做了,我写了下面一段code的工作了这一点:
3> Now that all important things are done , i wrote the following piece of code to work this out :
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;
}
希望这将帮助人们经历同样的过程,而谁被卡住没有多少运气。干杯!
Hopefully, this will help people who are stuck without much luck while going through the same process. Cheers !
这篇关于问题在阅读中的Android Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!