Android : Apache POI duplicate entry: org/apache/xmlbeans/xml/stream/Location.class error

前端 未结 5 1621
庸人自扰
庸人自扰 2020-12-12 02:29

Hi I am getting following error while running my android project :

Error:Execution failed for task \':app:transformClassesWithJarMergingForDebug\'.
> com.         


        
5条回答
  •  萌比男神i
    2020-12-12 02:49

    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:

    
    
    
        
    
        

    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 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 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...

提交回复
热议问题