2

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);
1
  • 2
    Well your first problem is that you can't get the resource for ncaa.xlsx - ins is null. So before you think about anything else, you should get that part working. Commented Jul 5, 2015 at 18:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick answer, but will handing-off method still work even when the app is published (where it can't access the file)?
@JacobPlatin: I am sorry, but I do not know what your comment is referring to. If you wish to use a library that only accepts a file, then you need a file. How you get that file is up to you. You indicated that you were packaging the spreadsheet as a raw resource, and I indicated that you would then have to copy the raw resource to a file. You are welcome to get the file by some other means (e.g., download it from the Internet). Or, do not ship a spreadsheet in the first place, but convert it into some data that is easier to process.
No problem, I actually answered my own question; I apologize
1

As explained fairly clearly in the Apache POI documentation, don't use an InputStream if you have a file! Using a file is both quicker and lower memory, so should be very much preferred over a stream, especially if you have a File to start with!

As per the Apache POI documentation on Files vs Streams, instead of your current code:

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

You should instead make it something like:

URL ins = this.getClass().getResource("ncaa.xlsx");
if (ins == null) 
    throw new FileNotFoundException("The XLSX file didn't exist on the classpath");
File file = new new File(ins.toURI());
OPCPackage pkg = OPCPackage.open(file);
XSSFWorkbook workbook = new XSSFWorkbook(pkg);

You'll want to keep the OPCPackage around until the end, so you can close it to release the file handle resources

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.