Jersey @Produces Apache XSSFWorkbook

守給你的承諾、 提交于 2019-12-06 04:18:47

You need to write your custom writer for XSSFWorkbook (or find one) and plug it into Jersey like :

@Provider
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
public class CustomXSSFWorkbookWriter implements MessageBodyWriter {
    //This methode is the most important for you
    @Override
public void writeTo(Object target, Class type, Type genericType,
        Annotation[] annotations, MediaType mediaType,
        MultivaluedMap httpHeaders, OutputStream outputStream)
        throws IOException {

Then you have to add the package of this class to your web.xml like this :

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.packages.to.your.views;com.packages.to.your.providers</param-value>
    </init-param>

Jersey will your your own writer to produce this specific format. Sorry i don't know anything about XSSFWorkbook.

Hopefully will solve your problem.

I was able to solve the problem by returning StreamingOutput via Jersey. Then I wrote out the bytes of StreamingOutput into an OutputStream. I got the byte[] from the OutputStream, wrote that OutputStream into the InputStream and then created a new XSSFWorkbook from that InputStream. I know this is a very dirty way of doing it. I will try the way suggested by -Camille R above for what I hope will be a cleaner solution. But in the meantime...

    @GET
    @Path("/excel/")
    @Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    public StreamingOutput exportReadingsAsExcel(@Context HttpServletResponse webResponse)
    {
        XSSFWorkbook excel = createExcel();
        setHeader(webResponse, "export.xlsx");
        StreamingOutput streamOutput = new StreamingOutput(){
            public void write(OutputStream output) throws IOException, WebApplicationException {
                try {
                    excel.write(output);
                } catch (Exception e) {
                    throw new WebApplicationException(e);
                }
            }
        };
        return streamOutput;
      }

On the receiver function:

private XSSFWorkbook createExcel(StreamingOutput mStream){
        XSSFWorkbook excel = new XSSFWorkbook();
        try {
            ExcelOutputStream out = new ExcelOutputStream();
            excel.write(out);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(out.getBytes());
            excel = new XSSFWorkbook(inputStream ;
        } catch (WebApplicationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    class MyOutputStream extends OutputStream {
        private ByteArrayOutputStream myByteArray;
        public MyOutputStream(){
            myByteArray = new ByteArrayOutputStream();
        }
        @Override
        public void write(int b) throws IOException {
            // TODO Auto-generated method stub
            myByteArray.write(b);
        }
        public byte[] getBytes(){
            return myByteArray.toByteArray();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!