问题描述
我试图用产生新泽西州XSSFWorkbook。
I am trying to produce a XSSFWorkbook using Jersey.
我曾尝试以下标题和似乎没有任何工作:
I have tried the following headers and nothing seems to work:
@Produces(应用程序/ XML)
@Produces("application/xml")
@Produces(应用程序/ vnd.openxmlformats-officedocument.s preadsheetml.sheet)
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@Produces(应用程序/ vnd.openxml
@Produces("application/vnd.openxml"
所有返回以下错误:
产生的原因:com.sun.jersey.api.MessageException:邮件正文作家Java类org.apache.poi.xssf.usermodel.XSSFWorkbook和Java类型类org.apache.poi.xssf.usermodel.XSSFWorkbook和MIME媒体类型application / XML没有被发现
... 37多个
基本上我有创造的XSSFWorkbook一个功能,我想将它写出来供用户下载。我能做到这一点的:
Essentially I have a function which creates the XSSFWorkbook and I would like to write it out for user to download. I am able to do it with:
/*
HttpServletResponse response;
XssfWorkbook excel.write(response.getOutputStream());
*/
@GET
@Path("/excel/")
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
public XSSFWorkbook exportReadingsAsExcel(@Context HttpServletResponse webResponse)
{
XSSFWorkbook excel = createExcel();
setHeader(webResponse, "export.xlsx");
try {
excel.write(webResponse.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return excel; //ERROR IS HERE
}
但我也需要函数返回XSSFWorkbook其他原因。而我希望,而不是使用网络响应我可以写新泽西出来。
But I also need the function to return the XSSFWorkbook for other reasons. And I was hoping instead of using the web response I could have Jersey write it out.
感谢您的帮助。
(我有点新的新泽西州和XSSF,所以请多多包涵,如果我关上我的术语或理解)
(I am somewhat new to Jersey and XSSF, so please bear with me if I am off on my terminology or understanding)
推荐答案
您需要编写XSSFWorkbook您的自定义作家(或找到一个),并把它插入到新泽西州这样的:
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 {
然后,你必须把这个类的包添加到您的的web.xml
是这样的:
<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>
新泽西将你自己的作家产生这种特定的格式。对不起,我不知道什么XSSFWorkbook
Jersey will your your own writer to produce this specific format. Sorry i don't know anything about XSSFWorkbook.
希望能解决你的问题。
这篇关于新泽西@Produces阿帕奇XSSFWorkbook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!