0

I have a method which returns StreamingOutput Object. And i want to write this StreamingOutput object into excel file. And i have written the following code, which is giving me the java.lang.ClassCastException Exception.

StreamingOutput stream = getStreamObject();
    SXSSFWorkbook workBook = (SXSSFWorkbook) stream; //Exception occurs here 

    FileOutputStream fileOut = new FileOutputStream("/save/excel/file/here");
    workBook.write(fileOut);
    fileOut.flush();
    fileOut.close();

So, Please help me out with this. Thank You in Advance.

4
  • 1
    right now you want to create a new object of type SXSSFWorkbook from the StreamingOutput. no wonder you get exception there. you should create a new row/cell and call stream.write() on the cell.setValue() Commented Dec 12, 2016 at 8:27
  • Just to be sure, did you rewrite that class? SxSSFWorkbook from apache doesn't seem to implement streamingoutput. Commented Dec 12, 2016 at 8:27
  • @Tosh No, I didn't rewrite the SXSSFWorkbook class. If apache doesn't implement StreamingOutput then is there any other way to write the StreamingOutput object into excel fil? By other way i mean any intermediate Object which can be convertable from StreamingOutput and can be used by SXSSFWorkbook to write into excel. Commented Dec 12, 2016 at 8:35
  • @YogeshPatel I didn't notice till it was too late to fix my comment. There is nothing in your code that necessitates the workbook, you could easily remove it and just save your streaming output to file. Assuming your streaming output is already properly formatted there is no reason for the typecast. If you need that for some reason that isn't obvious, you should either rewrite your question or create a new post for how to format your output to an xml file. Commented Dec 12, 2016 at 9:02

1 Answer 1

2

StreamingOutput write method must be overridden to return the output object. Here is an example :

public Response streamExample(){
        StreamingOutput stream = new StreamingOutput() {
            @Override
            public void write(OutputStream out) throws IOException, WebApplicationException {
                Writer writer = new BufferedWriter(new OutputStreamWriter(out));
                for (int i = 0; i < 10000000 ; i++){
                    writer.write(i + " ");
                }
                writer.flush();
            }
        };
        return Response.ok(stream).build();
    }

Please check the Docs of Streaming output and here for more information

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.