Android本地预览Excel,Word,PPT,PDF[解决Androidaspose。。。Android 本地预览Excel,Word,PPT,PDF
解决Android asposed转Excel乱码问题
近期获得⼀个需求, 要求做⼀个类似WPS的功能, 不过只需要预览不需要编辑
Android本⾝没有提供预览Office的组件, 百度后发现腾讯TBS可以做到这⼀点,试了下,效果不尽⼈意;
附腾讯TBS链接 :
我采⽤的⽅案是⾸先通过aspose将excel,word,ppt转为pdf, 然后⽤PdfViewer打开;
需要引⼊cells , slides ,words 三个Jar包, 分别对应 Excel , PPT , Word ;
Jar包连带源码都下最后附上链接, jar包也可以直接⽤aspose官⽹下载;
运⾏效果实例
部分代码实例 :
选择⽂件【调⽤系统的⽂件管理】
/**
* 选择⽂件【调⽤系统的⽂件管理】
*/
private fun chooseFile(){
startActivityForResult(Intent().apply{
action = Intent.ACTION_GET_CONTENT
type ="*/*"
addCategory(Intent.CATEGORY_OPENABLE)
}, REQUEST_CHOOSE_FILE)
}document有安卓版吗
onActivityResult
//  选择⽂件返回
override fun onActivityResult(requestCode: Int, resultCode: Int,data: Intent?){
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_CHOOSE_FILE){
data?.let{
val uri = it.data
val path = Instance(this).getChooseFileResultPath(uri)
openFile(path)
}
}
}
}
openFile
private fun openFile(path: String){
loading.visibility = View.VISIBLE
GlobalScope.launch{
val realPath =getRealPath(path)
runOnUiThread {
loading.visibility = View.INVISIBLE
if(TextUtils.isEmpty(realPath)){
ToastUtils.showShort("格式不⽀持")
return@runOnUiThread
}
pdfView.fromFile(File(realPath))
.pages(0,2,1,3,3,3)// all pages are displayed by default
.enableSwipe(true)// allows to block changing pages using swipe
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
.onError{
ToastUtils.showLong("onError: ${it.message} ")
}
.onTap{false}
.enableAnnotationRendering(false)// render annotations (such as comments, colors or forms)
.
password(null)
.scrollHandle(null)
.enableAntialiasing(true)// improve rendering a little bit on low-res screens
// spacing between pages in dp. To define spacing color, set view background
.spacing(0)
.autoSpacing(false)// add dynamic spacing to fit each page on its own on the screen
.linkHandler{}
.pageFitPolicy(FitPolicy.WIDTH)// mode to fit pages in the view
.fitEachPage(false)// fit each page to the view, else smaller pages are scaled relative to largest page.
.pageSnap(false)// snap pages to screen boundaries
.pageFling(false)// make a fling change only a single page like ViewPager
.
nightMode(false)// toggle night mode
.load()
}
}
}
getRealPath
/**
* 获取PDF路径
*/
private fun getRealPath(path: String): String {
//  ⾸先需要判断原始类型是否为PDF
val type =getFileType(path)
val fileName =getFileName(path)
//  如果是PDF 直接返回
if(type =="pdf"){
return path
}
val realPath = tempDir + File.separator + fileName + type +".pdf" //  如果已存在直接返回
if(File(realPath).exists()){
return realPath
}
//  Word
if(type =="doc"|| type =="docx"){
try{
val document =Document(path)
document.save(realPath, SaveFormat.PDF)
return realPath
}catch(e: Exception){
e.printStackTrace()
}
//  Excel
}else if(type =="xlsx"|| type =="xls"){
try{
return realPath
}catch(e: Exception){
e.printStackTrace()
}
//  PPT
}else if(type =="ppt"|| type =="pptx"){
try{
AsposedUtils.ppt2pdf(path, realPath)
return realPath
}catch(e: Exception){
e.printStackTrace()
}
}
return""
}
解决aspose excel转换后中⽂乱码问题
复制中⽂字体[随便选⼀个, 我选的微软雅⿊]到assets中将assets中的字体拷贝到储存中
转换Excel时, 将字体传给Workbook
tempDir 下有微软雅⿊字体
fun excel2pdf(sourceFilePath: String, desFilePath: String){ try{
//  中⽂乱码处理主要是设置微软雅⿊字体
val configs =IndividualFontConfigs()
configs.setFontFolder(tempDir,true)
val loadOptions =LoadOptions()
loadOptions.fontConfigs = configs
//  loadOptions.languageCode = CountryCode.CHINA val pdfSaveOptions =PdfSaveOptions()
//  原始excel路径
val wb =Workbook(sourceFilePath, loadOptions)
val fileOS =FileOutputStream(desFilePath)
//  数组为⼏就展⽰⼏页
val showSheets =intArrayOf(unt)
/
/  隐藏workbook中不需要的sheet页。
//  printSheetPage(wb, showSheets)
wb.save(fileOS, pdfSaveOptions)
fileOS.flush()
fileOS.close()
println("完毕")
}catch(e: Exception){
e.printStackTrace()
}
}