1

I am following the tutorial(https://www.blueappsoftware.com/how-to-read-excel-file-in-android-tutorial/) to make my android app can read excel file by line, everything works well except Apache POI, it throws out an exception

Cause: duplicate entry: org/apache/xmlbeans/xml/stream/Location.class org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithStackFramesFixerForDebug'.

I did some searches in StackOverflow as well, I downloaded the two jar files from https://github.com/andruhon/android5xlsx and then manually added them into my project library. gradle file will change to

implementation files('poi-3.12-android-a.jar') implementation files('poi-ooxml-schemas-3.12-20150511-a.jar'

but when I run build, it throws the error

Cause: duplicate entry: com/

Another solution I have tried is to enable multiDex and add

dexOptions {
    preDexLibraries false
    incremental false
    javaMaxHeapSize "3072m"
}, but nothing happened...

but nothing happened...Could anyone help me?

Below is code

Dependencies:

implementation "org.apache.poi:poi:3.17"
implementation "org.apache.poi:poi-ooxml:3.17"

activity-main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    tools:context="blueappsoftware.readexcelfile.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Excel Data below -"
        android:id="@+id/textview"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:lineSpacingExtra="2dp"
        android:gravity="center" />

</LinearLayout>

MainActivity

package blueappsoftware.readexcelfile;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
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 {
    String TAG ="main";
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textview);
        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;
            textView.append("\n");
            while (rowIter.hasNext()) {
                Log.e(TAG, " row no "+ rowno );
                HSSFRow myRow = (HSSFRow) rowIter.next();
                if(rowno !=0) {
                    Iterator<Cell> cellIter = myRow.cellIterator();
                    int colno =0;
                    String sno="", date="", det="";
                    while (cellIter.hasNext()) {
                        HSSFCell myCell = (HSSFCell) cellIter.next();
                        if (colno==0){
                            sno = myCell.toString();
                        }else if (colno==1){
                            date = myCell.toString();
                        }else if (colno==2){
                            det = myCell.toString();
                        }
                        colno++;
                        Log.e(TAG, " Index :" + myCell.getColumnIndex() + " -- " + myCell.toString());
                    }
                    textView.append( sno + " -- "+ date+ "  -- "+ det+"\n");
                }
                rowno++;
            }
        } catch (Exception e) {
            Log.e(TAG, "error "+ e.toString());
        }
    }
}
HungryBird
  • 1,077
  • 1
  • 11
  • 28
  • @centic I downloaded the jar file from https://github.com/andruhon/android5xlsx, and then manually add the jar as the library in my project. When I build my project via Gradle, it returns Cause: duplicate entry: com/, can you help? – HungryBird Nov 20 '19 at 10:48

0 Answers0