I am working for my university project. I have searched a lot but don't know why the excel is not reading. I have added this jar file in Android Studio. Followed this tutorial. Actually I am confused. But I have added some logs and toasts as you see. They are showing nothing. I have debugged it, in fact in the case of debug, it shows nothing. Can you please suggest me a good way to read and write Excel or any code snippet?
The code is as follows:
if (!isExternalStorageAvailable() || isExternalStorageReadOnly())
{
Log.w("FileUtils", "Storage not available or read only");
return;
}
try{
// Creating Input Stream
sharedPreferences = MyApplication.preferences;
final int projectID = sharedPreferences.getInt("count", 0);
int taskID = 0;
final String count = sharedPreferences.getString("project", projectFile);
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"
+ mainFolder + "/" + count,"/task.xls");
Log.e("full file",file.toString());
FileInputStream myInput = new FileInputStream(file);
Log.e("full file",myInput.toString());
// Create a POIFSFileSystem object
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
Log.e("full file",myFileSystem.toString());
// Create a workbook using the File System
HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
Log.e("full file",myWorkBook.toString());
// Workbook wb = WorkbookUtil.create(new File("MyExcel.xls"));
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Log.e("full file",mySheet.toString());
/** We now need something to iterate through the cells.**/
Iterator<Row> rowIter = mySheet.rowIterator();
HSSFRow nameRow = mySheet.getRow(1);
String name = nameRow.getCell(0).toString();
projectname.setText(name);
Log.e("full file",name.toString());
while(rowIter.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator<Cell> cellIter = myRow.cellIterator();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
Log.w("FileUtils", "Cell Value: " + myCell.toString());
Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){e.printStackTrace(); }
return;
The logs stop working after this line:
FileInputStream myInput = new FileInputStream(file);
Log file is:
11-24 12:02:46.993 9177-9196/? E/linker: "/system/bin/app_process32": ignoring 2-entry DT_PREINIT_ARRAY in shared library!
11-24 12:02:48.055 9177-9267/? E/fb4a(:<default>):0lk: E1124 12:02:48.000000 -1703212752 xplat/liger/src/proxygen/facebook/httpclient/java/src/cpp/HTTPClient.cpp:590] failed to get socket address from :53
11-24 12:02:48.062 9177-9267/? E/fb: failed to get socket address from :53
11-24 12:02:50.857 8986-8986/razon.language E/razon.language.Home$6: selected file /storage/emulated/0/Language/Project3/task.xls
11-24 12:02:50.857 8986-8986/razon.language E/moved: Moved To /Language/Project3/
11-24 12:02:50.862 8986-8986/razon.language E/full file: /storage/emulated/0/Language/Project3/task.xls
11-24 12:02:50.862 8986-8986/razon.language E/full file: java.io.FileInputStream@2f04e7c
I'm writing this based on last stack trace of yours which was,
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
As the trace message suggests, the file you're trying to read out the data from has a format supported from Office 2007 and onwards. POI has this component named XSSF( see the very basic in first paragraph ) that can be used to handle such documents.
So, technically speaking, you need to replace your HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell
etc to XSSFWorkbook, XSSFSheet, XSSFRow, XSSFCell
and forth on. As for the following,
POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
POIFSFileSystem
(or similarly, NPOIFSFileSystem) is only used with .xls documents. The equivalent for .xlsx documents is OPCPackage
( example ). It builds out of a File directly thus, your code should look like this.
OPCPackage pkg = OPCPackage.open(file);
Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.