How can I get an Input Stream from HSSFWorkbook Object

the method close() is undefined for the type hssfworkbook
xssfworkbook example
org apache poi xssf usermodel xssfworkbook api
hssfworkbook documentation
xssfworkbook cannot be resolved to a type
convert inputstream to excel java
import hssfworkbook

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 =
                out.write(buffer, 0, count);
        }   catch (IOException ioe) { 
            System.err.println("IOException in Download::sendFile"); 
        } 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 

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

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

// this part is important to let the browser know what you're sending
// 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 {
catch (IOException ioe) { 
  // if this happens there is probably no way to report the error to the user
  if (!response.isCommited()) {
    // show response text now

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

How can I get an Input Stream from HSSFWorkbook Object, Get an Input Stream from HSSFWorkbook Object. Code example extracted from Stack Overflow: // this part is important to let the browser know what you're sending� In my savefile method. InputStream inptest= **workbook.getStream** ps.setBinaryStream(2,fin,fin.available()); So inptest variable accepts InputStream which I wanted to get it from Workbook. java excel apache-poi. share.

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());

HSSFWorkbook (POI API Documentation), InputStream s, boolean preserveNodes) Given a POI POIFSFileSystem object, read in its Workbook along with all related nodes, and populate the high and� fs - the POI filesystem that contains the Workbook stream. preserveNodes - whether to preserve other nodes, such as macros. This takes more memory, so only say yes if you need to. If set, will store all of the POIFSFileSystem in memory Throws: - if the stream cannot be read See Also: POIFSFileSystem; HSSFWorkbook

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();

        // Close the document

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

🥝 How to get input stream from an HSSFWorkbook object, The problem with your question is that you are mixing OutputStreams and InputStreams. An InputStream is what you read, and an OutputStream is what you � to HSSFWorkbook class ,but it cause null pointer exception, i am not sure if i am doing it correctly, so if you can suggest right way to do that, that will be great help, My requirement is User will upload Excel file and we need to read data from excel file,

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

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

HSSFWorkbook wb = new HSSFWorkBook();

ServletOutputStream out = response.getOutputStream();
try {
catch (IOException ioe) { 

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

Read / Write Excel file (.xls or .xlsx) using Apache POI � GitHub, InputStream ExcelFileToRead = new FileInputStream("C:/Test.xls");. HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);. HSSFSheet sheet =wb. Note that only objects of classes that implement the interface can be written to and read from an output/input stream. Serializable is a marker interface, which doesn’t define any methods. Only objects that are marked ‘serializable’ can be used with ObjectOutputStream and ObjectInputStream.

Apache POI �€“ Core Classes, Creates a new HSSFWorkbook object using an input stream. 5. HSSFWorkbook( s, boolean preserveNodes). Constructs a POI file system� HSSFWorkbook(DirectoryNode directory, POIFSFileSystem fs, boolean preserveNodes) Given a POIFSFileSystem object and a specific directory within it, it creates an SSFWorkbook object to read a specified workbook. 4: HSSFWorkbook( s) Creates a new HSSFWorkbook object using an input stream. 5: HSSFWorkbook( s

How to Read Excel Files in Java using Apache POI, Workbook workbook = new XSSFWorkbook(inputStream);. Sheet firstSheet A More Object-Oriented Example to read Excel File. For nicer and� Register an object to be validated before the graph is returned. While similar to resolveObject these validations are called after the entire graph has been reconstituted. Typically, a readObject method will register the object with the stream so that when all of the objects are restored a final set of validations can be performed.

Read & Write Data from Excel File in Selenium Webdriver: POI & JXL, To read XLS files, an HSSF implementation is provided by POI library. To read //Close input stream inputStream.close(); //Create an object of� It sets readlimit i.e. maximum number of bytes that can be read before mark position becomes invalid. Syntax : public void mark(int arg) Parameters : arg : integer specifying the read limit of the input Stream Return : void; read() : reads next byte of data from the Input Stream. The value byte is returned in the

  • +1 for example. But how I can get file size of HSSFWorkbook? I want to add a header Content-Length
  • @MyTitle I think if you wanted to set a content-length header you'd need to write the stream to a ByteArrayOutputStream first, then get its length, then write it to the HTTP response.