[ACCEPTED]-How can I get an Input Stream from HSSFWorkbook Object-apache-poi

Accepted answer
Score: 10

The problem with your question is that you 14 are mixing OutputStreams and InputStreams. An 13 InputStream is something you read from and 12 an OutputStream is something you write to.

This 11 is how I write a POI object to the output 10 stream.

// this part is important to let the browser know what you're sending
response.setContentType("application/vnd.ms-excel");
// the next two lines make the report a downloadable file;
// leave this out if you want IE to show the file in the browser window
String fileName = "Blah_Report.xls";
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 

// get the workbook from wherever
HSSFWorkbook wb = getWorkbook();
OutputStream out = response.getOutputStream();
try {
   wb.write(out);
}       
catch (IOException ioe) { 
  // if this happens there is probably no way to report the error to the user
  if (!response.isCommited()) {
    response.setContentType("text/html");
    // show response text now
  }
}

If you wanted to re-use your existing 9 code you'd have to store the POI data somewhere 8 then turn THAT into an input stream. That'd 7 be easily done by writing it to a ByteArrayOutputStream, then 6 reading those bytes using a ByteArrayInputStream, but 5 I wouldn't recommend it. Your existing 4 method would be more useful as a generic 3 Pipe implementation, where you can pipe 2 the data from an InputStream to and OutputStream, but 1 you don't need it for writing POI objects.

Score: 2

you can create a InputStream from a object.

public InputStream generateApplicationsExcel() {
    HSSFWorkbook wb = new HSSFWorkbook();
    // Populate a InputStream from the excel object
    return new ByteArrayInputStream(excelFile.getBytes());
}

0

Score: 2

My solution is to transfer the HSSFWorkbook 3 to ByteArrayOutputStream first, and then 2 create an InputStream from ByteArrayOutputStream 1 :

        HSSFWorkbook wb = ...

        // Fill an empty output stream
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        wb.write(baos);

        // Close the document
        wb.close();

        // Create the input stream (do not forget to close the inputStream after use)
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
Score: 0

I think I understand what you're trying 10 to do (maybe I am undershooting, though)

you 9 don't really need that much code - check 8 out the write method -

HSSFWorkbook wb = new HSSFWorkBook();
//populate

ServletOutputStream out = response.getOutputStream();
try {
   wb.write(out);
   out.flush();
}       
catch (IOException ioe) { 
   //whatever
}
out.close();

As far as I remember 7 when I worked w/ POI that's what I did. If 6 you're inside a web framework you may have 5 to finaggle it so that the framework doesn't 4 try to do something with the that ServletOutputStream 3 after you've closed it. If it tries, you'll 2 get an exception throwing telling you that 1 the output stream is closed already.

More Related questions