ApachePOI导出Office文件到HttpServletResponse(网页下载)报错解决

2023-10-24 376点热度 0人点赞 0条评论

使用Spring提供的一个下载Office文档的接口报错(具体是使用Apache POI生成OfficeExcel和Word文件),直接输出到HttpServletResponse的OutputStream流,如下报错:

org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : The part /docProps/core.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller@f598ff7
    at org.apache.poi.openxml4j.opc.ZipPackage.saveImpl(ZipPackage.java:554)
    at org.apache.poi.openxml4j.opc.OPCPackage.save(OPCPackage.java:1505)
    at org.apache.poi.ooxml.POIXMLDocument.write(POIXMLDocument.java:242)

直接生成的Office(World或者Excel),输出到HttpServletResponse的OutputStream流

改成套一个BufferedOutputStream,起到一个缓存的作用,例如:

OutputStream out = response.getOutputStream();
template.writeAndClose(new BufferedOutputStream(out)); // 原来是 template.writeAndClose(out);
out.flush();

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论