Я пытаюсь прочитать файл excel с помощью библиотеки Apache POI, приложение представляет собой две кнопки: одна для выбора файла и одна для его чтения, каждый раз, когда я создаю экземпляр класса Workbook
, я получаю исключение SAXNotRecognizedException: http://xml.org/sax/properties/declaration-handler
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.excelreader, PID: 2929
java.lang.RuntimeException: http://xml.org/sax/properties/declaration-handler
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.<init>(Locale.java:3397)
at org.apache.xmlbeans.impl.store.Locale$XmlReaderSaxLoader.<init>(Locale.java:3087)
at org.apache.xmlbeans.impl.store.Locale.getSaxLoader(Locale.java:3072)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1272)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1259)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source:6)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
at org.apache.poi.xssf.usermodel.-$$Lambda$gobcPWQfEUc0tCYBAe7wHcxGUMU.init(Unknown Source:2)
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:684)
at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:288)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:97)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:125)
at org.apache.poi.xssf.usermodel.-$$Lambda$iuO6AyjZtGZgoUB0mS-ZEceWnhc.apply(Unknown Source:8)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:327)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:295)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:276)
at com.example.excelreader.MainActivity.readExcelFile(MainActivity.kt:127)
at com.example.excelreader.MainActivity.access$readExcelFile(MainActivity.kt:22)
at com.example.excelreader.MainActivity$onCreate$2.onClick(MainActivity.kt:55)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14970)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29363)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7860)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: org.xml.sax.SAXNotRecognizedException: http://xml.org/sax/properties/declaration-handler
at org.apache.harmony.xml.ExpatReader.setProperty(ExpatReader.java:168)
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.<init>(Locale.java:3391)
at org.apache.xmlbeans.impl.store.Locale$XmlReaderSaxLoader.<init>(Locale.java:3087)
at org.apache.xmlbeans.impl.store.Locale.getSaxLoader(Locale.java:3072)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1272)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1259)
at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument$Factory.parse(Unknown Source:6)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:86)
at org.apache.poi.xssf.usermodel.-$$Lambda$gobcPWQfEUc0tCYBAe7wHcxGUMU.init(Unknown Source:2)
at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:684)
at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:180)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:288)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:97)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:125)
at org.apache.poi.xssf.usermodel.-$$Lambda$iuO6AyjZtGZgoUB0mS-ZEceWnhc.apply(Unknown Source:8)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:327)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:295)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:276)
at com.example.excelreader.MainActivity.readExcelFile(MainActivity.kt:127)
at com.example.excelreader.MainActivity.access$readExcelFile(MainActivity.kt:22)
at com.example.excelreader.MainActivity$onCreate$2.onClick(MainActivity.kt:55)
at android.view.View.performClick(View.java:7870)
at android.widget.TextView.performClick(TextView.java:14970)
at android.view.View.performClickInternal(View.java:7839)
at android.view.View.access$3600(View.java:886)
at android.view.View$PerformClick.run(View.java:29363)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7860)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
вот мой код:
package com.example.excelreader
import android.app.Activity
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.Log.d
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.WorkbookFactory
import org.xml.sax.SAXNotRecognizedException
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
private const val DOCUMENT_PICKING_REQUEST=1000
const val TAG="te~st"
class MainActivity : AppCompatActivity() {
private var targetFile: File?=null
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode==DOCUMENT_PICKING_REQUEST){
if(resultCode== Activity.RESULT_OK){
val path= data?.data
if(path!=null){
targetFile=createFileFromURI(path)
}
Toast.makeText(this, "document selected", Toast.LENGTH_SHORT).show()
}else{
Toast.makeText(this, " no document selected", Toast.LENGTH_SHORT).show()
}
}
super.onActivityResult(requestCode, resultCode, data)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
docPickButton.setOnClickListener {
dispatchFilePickIntent()
}
readButton.setOnClickListener {
readExcelFile()
}
}
private fun dispatchFilePickIntent(){
val intent= Intent(Intent.ACTION_GET_CONTENT)
intent.type="*/*"
startActivityForResult(intent, DOCUMENT_PICKING_REQUEST)
}
private fun createFileFromURI(source: Uri): File?{
val inputStream=contentResolver.openInputStream(source)
val file=createExcelFile()
if(inputStream!=null && file!=null){
val outputStream= FileOutputStream(file)
var length=0
while (length!= -1){
length=inputStream.read()
outputStream.write(length)
}
inputStream.close()
outputStream.close()
}
return file
}
private fun readFromInputStream(inputStream:InputStream){
val workbook= WorkbookFactory.create(inputStream)
val sheet=workbook.getSheetAt(0)
val row1=sheet.getRow(0)
val iterator=row1.cellIterator()
val cells= arrayListOf<Cell>()
while (iterator.hasNext()){
val cell= iterator.next()
cells.add(cell)
}
workbook.close()
d(TAG, cells.toString())
}
private fun createDirectory(): File?{
val directory= File(filesDir,"AppExcelFiles")
return if(directory.exists()){
directory
}else{
if(directory.mkdir()){
directory
}else{
null
}
}
}
private fun createExcelFile(): File?{
val directory=createDirectory()
return if(directory!=null){
val file= File(directory,"MyExcelFile.xlsx")
file
}else{
null
}
}
private fun readExcelFile(){
val workbook= WorkbookFactory.create(targetFile)
val sheet=workbook.getSheetAt(0)
val row1=sheet.getRow(0)
val iterator=row1.cellIterator()
val cells= arrayListOf<Cell>()
while (iterator.hasNext()){
val cell= iterator.next()
cells.add(cell)
}
workbook.close()
d(TAG, cells.toString())
}
}
Я предполагаю, что проблема в библиотеке XmlBeans, но я действительно не могу понять, как решить эту проблему, которую я пытался изменить версия библиотеки, но проблема остается, любая помощь будет очень признательна, заранее спасибо