Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.apache.poi.ooxml.POIXMLException #106

Closed
YarkinKurt opened this issue Jul 20, 2023 · 2 comments
Closed

org.apache.poi.ooxml.POIXMLException #106

YarkinKurt opened this issue Jul 20, 2023 · 2 comments

Comments

@YarkinKurt
Copy link

In my android application I am getting the following error:

org.apache.poi.ooxml.POIXMLException: http://xml.org/sax/properties/declaration-handler
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:66)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:657)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:286)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:88)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:135)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at java.lang.reflect.Method.invoke(Native Method)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:339)
2023-07-20 12:09:59.478  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.ss.usermodel.WorkbookFactory.createXSSFWorkbook(WorkbookFactory.java:314)
2023-07-20 12:09:59.479  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:232)
2023-07-20 12:09:59.479  3957-3957  System.err              com.example.myapplication            W  	at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:198)
2023-07-20 12:09:59.479  3957-3957  System.err              com.example.myapplication            W  	at com.example.myapplication.ChecklistPage.writeIntoFile(ChecklistPage.java:227)
2023-07-20 12:09:59.479  3957-3957  System.err              com.example.myapplication            W  	at com.example.myapplication.ChecklistPage$4.onClick(ChecklistPage.java:124)

Currently I am using apache poi 4.0.1 (no shadow poi) and I followed the steps mentioned #10 So my dependencies are:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
    implementation 'androidx.navigation:navigation-fragment:2.5.2'
    implementation 'androidx.navigation:navigation-ui:2.5.2'
    implementation 'org.apache.poi:poi-ooxml:4.0.1'
    implementation 'org.apache.commons:commons-collections4:4.1'
    implementation 'org.apache.commons:commons-collections4:4.1-javadoc'
    implementation 'stax:stax-api:1.0.1'
    implementation 'com.fasterxml:aalto-xml:1.3.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

I also added 3 lines mentined in #10 which are:

        System.setProperty("org.apache.poi.javax.xml.stream.XMLOutputFactory", "com.fasterxml.aalto.stax.OutputFactoryImpl");
        System.setProperty("org.apache.poi.javax.xml.stream.XMLEventFactory", "com.fasterxml.aalto.stax.EventFactoryImpl");
        System.setProperty("org.apache.poi.javax.xml.stream.XMLStreamReader", "com.fasterxml.aalto.stax.StreamReaderImpl");

And my code where I get the error is:

 try(FileInputStream input = new FileInputStream(Environment.getExternalStorageDirectory()+"/yeni.xlsx")) {
            Workbook workbook = WorkbookFactory.create(input);
            int i = workbook.getSheetIndex("Bob");
            Log.d(LOG,"Bob is at "+i);
         
            Sheet sheet1 = workbook.getSheetAt(i);

            Row row1 = sheet1.getRow(1);
            Row row2 = sheet1.getRow(2);
            Row row3 = sheet1.getRow(3);

            Cell cell1 = row1.getCell(4);
            Cell cell2 = row2.getCell(4);
            Cell cell3 = row3.getCell(4);

            cell1.setCellType(CellType.STRING);
            cell2.setCellType(CellType.STRING);
            cell3.setCellType(CellType.STRING);

            cell1.setCellValue(report.getGeneralInfo().informations.get(1).getValue());
            cell2.setCellValue(report.getGeneralInfo().informations.get(2).getValue());
            cell3.setCellValue(report.getCheckList().checks.get(1).getResultExplanation());


            FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory()+"/yeni.xlsx");
            workbook.write(fileOutputStream);

            if(fileOutputStream != null){
                fileOutputStream.flush();
                fileOutputStream.close();
                input.close();
                workbook.close();
            }

        }catch (Exception e){
            e.printStackTrace();
        }

Afaik my code is correct in terms of syntax. I do not get any error when I try to create a new .xlsx file and write into it. My problem only occurs when I try to write into an existing .xlsx file using FileInputStream. I suppose I have a problem with dependencies so how can I integrate shadow poi version which uses apache poi 4.0.1 into my project ?(it was mentioned in #90) I am also open to any other suggestions you can make.

@centic9
Copy link
Owner

centic9 commented Jul 26, 2023

There is some problem with XML Parsing. There should be a nested exception in the output that seems to be missing, so you might get more information if you debug and inspect the exception at that point.

However I strongly suggest that you base your project on the latest version of this Repo (based on Apache POI 5.2.3) and use the approach with shadow-jar. This had a number of reasons and you would likely run into all of them one-by-one by trying to do it without.

@YarkinKurt
Copy link
Author

I have to work on a lower poi version because I am writing an app for a device which uses Android 7. While browsing I found this repo https://github.com/andruhon/android5xlsx and it solved my problem. Thanks for the reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants