Reading from Excel file

I am going through my first experience writing an app using Kotlin and Android Studio. Whereas most things worked well or I was able to troubleshoot them (including writing an Excel file) I am not stuck for more than one day on trying to read data from an Excel file. I can compile the program but the app crashes when I try to run the part of the code linked to reading from Excel and more particularly the line using Workbookfactory. Here’s the part of the code that gives an issue (print statements are just to allow me to see where the issue is) :

        println("Exceloutput function was called")
        val input = FileInputStream("/sdcard/Documents/test.xlsx")
        println("Exceloutput function did not crash")
        var xlwb = WorkbookFactory.create(input)
        println("Exceloutput function did not crash 2")
        val xlws = xlwb.getSheetAt(0)
        println("Exceloutput function did not crash 3")
        xlws.createRow(0).createCell(0).setCellValue("5")
        println("Exceloutput function did not crash 4")
        xlwb.close()

the libraries I’ve imported :

import android.os.Bundle
import android.os.Environment
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.apache.poi.xssf.streaming.SXSSFEvaluationWorkbook.create
import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook.create
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.poi.xssf.usermodel.XSSFWorkbookFactory
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.FileWriter
import java.net.URI.create
The dependencies I defined in build.gradle :

dependencies {

implementation ‘androidx.core:core-ktx:1.7.0’
implementation ‘androidx.appcompat:appcompat:1.4.1’
implementation ‘com.google.android.material:material:1.5.0’
implementation ‘androidx.constraintlayout:constraintlayout:2.1.3’
implementation(“org.apache.poi:poi:5.2.2”)
implementation(“org.apache.poi:poi-ooxml:5.2.2”)
implementation(“org.apache.logging.log4j:log4j-api:2.17.2”)
implementation(“org.apache.logging.log4j:log4j-core:2.17.2”)
implementation(“org.apache.xmlbeans:xmlbeans:5.0.3”)
testImplementation ‘junit:junit:4.13.2’
androidTestImplementation ‘androidx.test.ext:junit:1.1.3’
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.4.0’
}

Error messages I get when the app crashes :

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.preflightchecklisy, PID: 16159 org.apache.poi.ooxml.POIXMLException: error: The ‘namespace-prefix’ feature is not supported while the ‘namespaces’ feature is enabled. at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:68) at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661) at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:259) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:118) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:98) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36) at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory$$ExternalSyntheticLambda4.create(Unknown Source:2) at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:329) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185) at com.example.PreFlightChecklist.MainActivity.onCreate$lambda-16$lambda-15$lambda-14$lambda-13$lambda-12$lambda-11$lambda-10$lambda-8(MainActivity.kt:193) at com.example.PreFlightChecklist.MainActivity.$r8$lambda$F-lh3xaeu6esj9SclhWcMZd_Rz4(Unknown Source:0) at com.example.PreFlightChecklist.MainActivity$$ExternalSyntheticLambda6.onClick(Unknown Source:0) at android.view.View.performClick(View.java:7792) at android.widget.TextView.performClick(TextView.java:16112) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View.performClickInternal(View.java:7769) at android.view.View.access$3800(View.java:910) at android.view.View$PerformClick.run(View.java:30213) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) Caused by: java.io.IOException: error: The ‘namespace-prefix’ feature is not supported while the ‘namespaces’ feature is enabled. at org.apache.poi.xssf.model.ThemesTable.readFrom(ThemesTable.java:121) at org.apache.poi.xssf.model.ThemesTable.(ThemesTable.java:87) at org.apache.poi.xssf.usermodel.XSSFRelation$$ExternalSyntheticLambda17.init(Unknown Source:2) at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61) at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661) at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:259) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:118) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:98) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36) at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory$$ExternalSyntheticLambda4.create(Unknown Source:2) at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:329) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185) at com.example.PreFlightChecklist.MainActivity.onCreate$lambda-16$lambda-15$lambda-14$lambda-13$lambda-12$lambda-11$lambda-10$lambda-8(MainActivity.kt:193) at com.example.PreFlightChecklist.MainActivity.$r8$lambda$F-lh3xaeu6esj9SclhWcMZd_Rz4(Unknown Source:0) at com.example.PreFlightChecklist.MainActivity$$ExternalSyntheticLambda6.onClick(Unknown Source:0) at android.view.View.performClick(View.java:7792) at android.widget.TextView.performClick(TextView.java:16112) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View.performClickInternal(View.java:7769) at android.view.View.access$3800(View.java:910) at android.view.View$PerformClick.run(View.java:30213) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) Caused by: org.apache.xmlbeans.XmlException: error: The ‘namespace-prefix’ feature is not supported while the ‘namespaces’ feature is enabled. at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:2550) at org.apache.xmlbeans.impl.store.Locale.lambda$parseToXmlObject$3(Locale.java:715) at org.apache.xmlbeans.impl.store.Locale$$ExternalSyntheticLambda4.parse(Unknown Source:6) at org.apache.xmlbeans.impl.store.Locale.syncWrap(Locale.java:488) at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:714) at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:233) at org.apache.xmlbeans.impl.schema.AbstractDocumentFactory.parse(AbstractDocumentFactory.java:71) at org.apache.poi.xssf.model.ThemesTable.readFrom(ThemesTable.java:119) at org.apache.poi.xssf.model.ThemesTable.(ThemesTable.java:87) at org.apache.poi.xssf.usermodel.XSSFRelation$$ExternalSyntheticLambda17.init(Unknown Source:2) at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61) at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661) at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:259) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:118) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:98) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36) at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory$$ExternalSyntheticLambda4.create(Unknown Source:2) at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:329) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185) at com.example.PreFlightChecklist.MainActivity.onCreate$lambda-16$lambda-15$lambda-14$lambda-13$lambda-12$lambda-11$lambda-10$lambda-8(MainActivity.kt:193) at com.example.PreFlightChecklist.MainActivity.$r8$lambda$F-lh3xaeu6esj9SclhWcMZd_Rz4(Unknown Source:0) at com.example.PreFlightChecklist.MainActivity$$ExternalSyntheticLambda6.onClick(Unknown Source:0) at android.view.View.performClick(View.java:7792) at android.widget.TextView.performClick(TextView.java:16112) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View.performClickInternal(View.java:7769) at android.view.View.access$3800(View.java:910) at android.view.View$PerformClick.run(View.java:30213) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) Caused by: org.xml.sax.SAXNotSupportedException: The ‘namespace-prefix’ feature is not supported while the ‘namespaces’ feature is enabled. at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:266) at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:2523) at org.apache.xmlbeans.impl.store.Locale.lambda$parseToXmlObject$3(Locale.java:715) at org.apache.xmlbeans.impl.store.Locale$$ExternalSyntheticLambda4.parse(Unknown Source:6) at org.apache.xmlbeans.impl.store.Locale.syncWrap(Locale.java:488) at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:714) at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:233) at org.apache.xmlbeans.impl.schema.AbstractDocumentFactory.parse(AbstractDocumentFactory.java:71) at org.apache.poi.xssf.model.ThemesTable.readFrom(ThemesTable.java:119) at org.apache.poi.xssf.model.ThemesTable.(ThemesTable.java:87) at org.apache.poi.xssf.usermodel.XSSFRelation$$ExternalSyntheticLambda17.init(Unknown Source:2) at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61) at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:661) at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165) at org.apache.poi.xssf.usermodel.XSSFWorkbook.(XSSFWorkbook.java:259) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:118) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:98) at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36) at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory$$ExternalSyntheticLambda4.create(Unknown Source:2) at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:329) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:224) at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:185) at com.example.PreFlightChecklist.MainActivity.onCreate$lambda-16$lambda-15$lambda-14$lambda-13$lambda-12$lambda-11$lambda-10$lambda-8(MainActivity.kt:193) at com.example.PreFlightChecklist.MainActivity.$r8$lambda$F-lh3xaeu6esj9SclhWcMZd_Rz4(Unknown Source:0) at com.example.PreFlightChecklist.MainActivity$$ExternalSyntheticLambda6.onClick(Unknown Source:0) at android.view.View.performClick(View.java:7792) at android.widget.TextView.performClick(TextView.java:16112) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View.performClickInternal(View.java:7769) at android.view.View.access$3800(View.java:910) at android.view.View$PerformClick.run(View.java:30213) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Quite impossible for me to figure that out since I am not an expert at all, more a newcomer to app programming. I tried searching on Internet but couldn’t find the solution so would appreciate some expert help. Thanks in advance !

The error is not in Kotlin code, but inside POI library. Try to check the library documentation. Also are you sure it works on Android?

1 Like

Thanks for your answer. According to examples I saw on YouTube it works with Android. The documentation of apache.poi is not very helpful but I can try and check again.