使用StreamingOutput对象写入excel文件。 (java.lang.ClassCastException)

我有一个返回StreamingOutput对象的方法。 而我想把这个StreamingOutput对象写入excel文件。 而且我写了下面的代码,它给了我java.lang.ClassCastExceptionexception。

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

所以,请帮我解决这个问题。 先谢谢你。

必须重写StreamingOutput写入方法以返回输出对象。 这里是一个例子:

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

请查看Streaming输出的文档,并在这里获取更多信息