转换的InputStream的FileInputStream的Apache POI不工作 [英] Converting InputStream to FileInputStream Not Working for Apache POI

查看:495
本文介绍了转换的InputStream的FileInputStream的Apache POI不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问从我RES /原始文件夹中的Excel文件,我知道,你真的只能做到这一点使用的InputStream;然而,XSSFWorkbook命令中的Apache POI只需要FileInputStreams而不是InputStreams:有没有办法做到这一点?我用下面的code尝试,但走这条错误:显示java.lang.NullPointerException:尝试调用虚拟方法java.net.URI中java.net.URL.toURI()上空对象引用

下面是code,我试过用:

 插件URL = this.getClass()的getResource(ncaa.xlsx);
 的FileInputStream文件=新的FileInputStream(新文件(ins.toURI()));
 XSSFWorkbook工作簿=新XSSFWorkbook(文件);


解决方案

  

有没有办法做到这一点?


复制出来的原始资源到一个文件,然后打开该文件,并交给了的FileInputStream 到库中。


  

我已经尝试使用以下


的getResource()无关与Android的资源。要访问的InputStream 上的原始资源,使用 getResources()。openRawResource(),叫上你的活动或其他方便的上下文

I am trying to access an excel file from my res/raw folder, and I know that you can only really do this using an InputStream; however, the XSSFWorkbook commands in Apache POI only take FileInputStreams and not InputStreams: is there any way to do this? I've tried using the following code but go this error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.net.URI java.net.URL.toURI()' on a null object reference

Here is the code I've tried using:

     URL ins = this.getClass().getResource("ncaa.xlsx");
 FileInputStream file = new FileInputStream(new File(ins.toURI()));
 XSSFWorkbook workbook = new XSSFWorkbook(file);

解决方案

is there any way to do this?

Copy the raw resource out to a file, then open the file and hand the FileInputStream over to the library.

I've tried using the following

getResource() has nothing to do with Android resources. To access an InputStream on a raw resource, use getResources().openRawResource(), called on your Activity or other handy Context.

这篇关于转换的InputStream的FileInputStream的Apache POI不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆