Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an Input Stream from HSSFWorkbook Object

I want my web application users to download some data as an Excel file.

I have the next function to send an Input Stream in the response object.

public static void sendFile(InputStream is, HttpServletResponse response) throws IOException {
        BufferedInputStream in = null;
        try {
            int count;
            byte[] buffer = new byte[BUFFER_SIZE];
            in = new BufferedInputStream(is);
            ServletOutputStream out = response.getOutputStream();
            while(-1 != (count = in.read(buffer)))
                out.write(buffer, 0, count);
            out.flush();            
        }   catch (IOException ioe) { 
            System.err.println("IOException in Download::sendFile"); 
            ioe.printStackTrace();
        } finally {
            if (in != null) {
                try { in.close(); 
                } catch (IOException ioe) { ioe.printStackTrace(); }
            }   
        }
    }

I would like to transform my HSSFWorkbook Object to an input stream and pass it to the previous method.

public InputStream generateApplicationsExcel() {
    HSSFWorkbook wb = new HSSFWorkbook();
    // Populate the excel object
    return null; // TODO. return the wb as InputStream 
}

http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFWorkbook.html

like image 701
Sergio del Amo Avatar asked Dec 18 '08 19:12

Sergio del Amo


People also ask

What is the difference between HSSFWorkbook and XSSFWorkbook?

HSSFWorkbook − This class has methods to read and write Microsoft Excel files in . xls format. It is compatible with MS-Office versions 97-2003. XSSFWorkbook − This class has methods to read and write Microsoft Excel and OpenOffice xml files in .

What is input stream object?

An input stream object is a source of bytes. The three most important input stream classes are istream , ifstream , and istringstream . The istream class is best used for sequential text-mode input. You can configure objects of class istream for buffered or unbuffered operation.

Which class does provide get input stream method?

Class InputStream. This abstract class is the superclass of all classes representing an input stream of bytes. Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.


2 Answers

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

        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());
like image 197
Laurent Avatar answered Oct 04 '22 12:10

Laurent


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());
}
like image 35
Xiong xiao Luo Avatar answered Oct 04 '22 10:10

Xiong xiao Luo