如何在Android中显示XLSX文件

问题描述 投票:0回答:1

我的 Android 资产文件夹中有一个 xlsx 文件。我需要在android中显示xlsx。如何从本地资源文件夹加载并显示 xlsx。

到目前为止使用 webview 尝试过的方法:

val excelFilePath = "file:///android_asset/asmls.xlsx"

loadUrl(excelFilePath)

当我浏览多个网站时,都提到Android Webview不直接支持xlsx。是否有任何解决方法或替代库可用于相同目的?

android kotlin webview android-webview xlsx
1个回答
0
投票

出于兼容性和性能原因,建议避免在应用程序中直接显示 XLSX 文件。通过使用具有适当 MIME 类型的意图选择器来选择更流畅的用户体验。这使用户能够使用他们喜欢的外部应用程序无缝打开文件。

val file = "file:///android_asset/asmls.xlsx"
val uri = Uri.parse(file)

val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

try {
    startActivity(Intent.createChooser(intent, "Choose an app to open the XLSX file"))
} catch (e: ActivityNotFoundException) {
    //handle no apps found eg inform the user
}

仔细检查 MIME 类型,可能需要将

android:usesCleartextTraffic
设置为
true

© www.soinside.com 2019 - 2024. All rights reserved.