As to help other people, here the complete working solution to read XLS files with POI v 3.17 (Android API 15 and later) (XLSX too but some lines to be changed).
First off all download the 0.5 release of the POI jar file form here: Releases and paste it to the /lib folder in Android Studio.
Your Layout with a simple TextView and a simple Button:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFEB3B"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="24dp"
android:background="#FFFFFF"
android:inputType="textMultiLine"
android:padding="24dp"
android:singleLine="false"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>
Then place this line in module gradle:
implementation files('libs/poishadow-all.jar')
Now (to try the app) create a sample excel file, 3 columns, 3 rows, any data you want. You can create more columns and sheet. But if you are trying this code first time, so please work with three columns only. now same the file name as “myexcelsheet.xls” Go to android project directory and open your android project. Go inside folder app -> src ->main. There you will see two folder name as java and res. Now create a new folder here name as assets and put myexcelsheet.xls file inside it.
Finally the Main Activity code:
package com.example.readexcelfiles;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import java.io.InputStream;
import java.util.Iterator;
public class MainActivity extends AppCompatActivity {
TextView txtView;
Button btnRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtView = findViewById(R.id.textView);
btnRead = findViewById(R.id.button);
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readExcelFileFromAssets();
}
});
}
public void readExcelFileFromAssets() {
try {
InputStream myInput;
// initialize asset manager
AssetManager assetManager = getAssets();
// open excel sheet
myInput = assetManager.open("myexcelsheet.xls");
// Create a POI File System 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();
int rowno =0;
txtView.append("\n");
while (rowIter.hasNext()) {
Log.e("aaa", " row no "+ rowno );
HSSFRow myRow = (HSSFRow) rowIter.next();
if(rowno !=0) {
Iterator<Cell> cellIter = myRow.cellIterator();
int colNum =0;
String sno="", date="", det="";
while (cellIter.hasNext()) {
HSSFCell myCell = (HSSFCell) cellIter.next();
if (colNum==0){
sno = myCell.toString();
}else if (colNum==1){
date = myCell.toString();
}else if (colNum==2){
det = myCell.toString();
}
colNum++;
Log.e("aaa", " Index :" + myCell.getColumnIndex() + " -- " + myCell.toString());
}
txtView.append( sno + " -- "+ date+ " -- "+ det+"\n");
}
rowno++;
}
} catch (Exception e) {
Log.e("aaa", "error "+ e.toString());
}
}
}
In order to read XLSX file, please read Mr. Kamal Bunkar comment:
First understand the notation.
XSSF (XML SpreadSheet Format) – Used to reading and writting Open Office XML (XLSX) format files.
HSSF (Horrible SpreadSheet Format) – Use to read and write Microsoft Excel (XLS) format files.
HWPF (Horrible Word Processor Format) – to read and write Microsoft Word 97 (DOC) format files.
In my tutorial I was using
// Get the first sheet from workbook
HSSFSheet mySheet = myWorkBook.getSheetAt(0);
Now to work with xlsx you should use this code
File myFile = new File(“C://temp/Employee.xlsx”);
FileInputStream fis = new FileInputStream(myFile);
// Finds the workbook instance for XLSX file
XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
// Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.getSheetAt(0);
// Get iterator to all the rows in current sheet
Iterator rowIterator = mySheet.iterator();
// Traversing over each row of XLSX file
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
// For each row, iterate through each columns
Iterator cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue() + “\t”);
break; case Cell.CELL_TYPE_NUMERIC:System.out.print(cell.getNumericCellValue() + “\t”);
break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue() + “\t”);
break; default : }
}
System.out.println(“”);
}
Obviously to be adapted to Android file location...
Hope to help...