8

I'm trying to read the xlsx file from asset folder. I received below exception,

05-16 10:12:05.613: E/AndroidRuntime(2915): FATAL EXCEPTION: main 05-16 10:12:05.613: E/AndroidRuntime(2915): java.lang.VerifyError: org/apache/poi/xssf/usermodel/XSSFWorkbook

before this exception, I received some warnings also like,

  • Could not find method org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument$Factory.parse, referenced from method org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead

  • VFY: unable to resolve exception class 3612 (Lorg/apache/xmlbeans/XmlException;)

I have added poi 3.12 library on my application, libraries screenshot as below,

enter image description here

And I have checked poi-3.12 and poi-ooxml-3.12 jar files in Order and Export, screenshot as below,

enter image description here

I used below code,

        InputStream is = context.getAssets().open("sample.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(is);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Cell cell = sheet.getRow(0).getCell(0);
        String value = cell.getStringCellValue() + "";

I want to read and write the .XLSX and .XLS files. How to resolve this issue?

Thanks in Advance.

bharath
  • 14,283
  • 16
  • 57
  • 95

2 Answers2

5

Firs you should connect to your project these jars.

jars you need

Then use this code for reading

public static void readXLSXFile() throws IOException
    {
        InputStream ExcelFileToRead = new FileInputStream("C:/Test.xlsx");
        XSSFWorkbook  wb = new XSSFWorkbook(ExcelFileToRead);

        XSSFWorkbook test = new XSSFWorkbook(); 

        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row; 
        XSSFCell cell;

        Iterator rows = sheet.rowIterator();

        while (rows.hasNext())
        {
            row=(XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            while (cells.hasNext())
            {
                cell=(XSSFCell) cells.next();

                if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
                {
                    System.out.print(cell.getStringCellValue()+" ");
                }
                else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
                {
                    System.out.print(cell.getNumericCellValue()+" ");
                }
                else
                {
                    //U Can Handel Boolean, Formula, Errors
                }
            }
            System.out.println();
        }

    }
sinitram
  • 524
  • 5
  • 14
  • Already I have added those jar files in project build libraries. – bharath May 16 '15 at 09:25
  • Can you tell please your ide? – sinitram May 16 '15 at 09:31
  • I'm using eclipse IDE. – bharath May 16 '15 at 09:43
  • So, try to remove all libraries and than connect again only these 4 jars. Maybe you have some duplication. – sinitram May 16 '15 at 09:49
  • If it doesn't work, download these 2 jars, and try with them. https://github.com/andruhon/AndroidReadXLSX/tree/master/libs – sinitram May 16 '15 at 09:51
  • Yeah I tried lot of times. But no use. AndroidReadXLSX is only for read XLSX file. We cant write XLSX file. – bharath May 16 '15 at 11:20
  • I also had a lot of problems when I've been working with this library, but I needed hust read the file. A know that these libraries work fine in java (not android). You should change jar files, and remove from libraries classes wich you don't use. – sinitram May 16 '15 at 12:24
  • Also you can just write a server in java (without android), and make your processing there. – sinitram May 16 '15 at 12:25
  • Above code is working fine in Java application. But same code is not working in Android. – bharath May 16 '15 at 13:13
  • Ya, I know. My Scientific Adviser told me that it is impossible work with this library in android, it also does not work with appcompat correctly. The best way is writing a server. – sinitram May 16 '15 at 13:50
0

Updated your code to able to read and write the .XLSX and .XLS files

InputStream is = context.getAssets().open("sample.xlsx"));
Workbook workbook = WorkbookFactory.create(is);
Sheet sheet = workbook.getSheetAt(0);
Cell cell = sheet.getRow(0).getCell(0);
String value = cell.getStringCellValue() + "";

For your libraries, please have a look here android getting java lang verify error when using external java lib

Community
  • 1
  • 1
jjcosare
  • 1,463
  • 9
  • 9