Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get InputStream from Apache's POI Workbook

Is there a way to obtain InputStream of Apache's POI Workbook?

I need it for piping to another OutputStream, however I'm unable to find such method (If it exists).

If it doesn't, any tips on alternative ways to obtain it?

like image 851
ioreskovic Avatar asked Jan 16 '14 08:01

ioreskovic


2 Answers

Here's how to implement #2 of Alexander Tokarev's answer (i.e get Inputstream from a workbook):

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        workbook.write(bos);
        byte[] barray = bos.toByteArray();
        InputStream is = new ByteArrayInputStream(barray);
    } catch (IOException e) {
        e.printStackTrace();
    }
like image 52
Kehinde Adedamola Shittu Avatar answered Oct 23 '22 10:10

Kehinde Adedamola Shittu


There's a several ways to solve this:

  1. You can use standard java PipetInputStream and PipedOutputStream. But you have to create different thread for using PipedInputStream (as described in http://docs.oracle.com/javase/7/docs/api/java/io/PipedInputStream.html)
  2. You can write the content to ByteArrayOutputStream, and then you can use resulting byte array via ByteArrayInputStream. This can be done sequentially in one thread.
like image 33
Alexander Tokarev Avatar answered Oct 23 '22 11:10

Alexander Tokarev