我用POI在xlsx文件中导出数据,并将它们添加到Zip文件中。打开zip时,我没有任何xlsx文件,只有三个目录(docProps、xl和_rels)和1个文件Content_Types xml。我认为这是xlsx文件的描述,但我不明白为什么。
代码:
public InputStream exportXlsx(List<MyObject> listeOfObject) throws IOException {
ByteArrayOutputStream excelOutputStreamZip = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(excelOutputStreamZip);
for (MyObject myObject : listeOfObject) {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet wsheet = wb.createSheet("mySheet");
XSSFRow row = wsheet.createRow(0);
XSSFCell cell = row.createCell(1);
cell.setCellValue(myObject.getValue1());
// Create all sheet and cell....
// Write WB conntent in outputStream
wb.write(excelOutputStreamZip);
addEntry(zip, myObject.getFileName(), excelOutputStreamZip);
}
InputStream inputStreamZipByte = new ByteArrayInputStream(
((ByteArrayOutputStream) excelOutputStreamZip).toByteArray());
zip.close();
return inputStreamZipByte;
}
public void addEntry(OutputStream zip, String filename, ByteArrayOutputStream os) {
byte[] bytes = os.toByteArray();
ZipEntry entry = new ZipEntry(filename);
Date d = new Date();
entry.setTime(d.getTime());
try {
((ZipOutputStream) zip).putNextEntry(entry);
((ZipOutputStream) zip).write(bytes);
((ZipOutputStream) zip).closeEntry();
} catch (IOException e) {
log.error("Can't read the file !", e);
} catch (ClassCastException cce) {
log.error("Bad format !", cce);
}
}此代码是在注入到Struts2操作中的服务中编写的。
struts.xml:
<action name="*MyAction" class="com.omb.view.action.myAction" method="{1}">
<result name="export" type="stream">
<param name="contentType">application/zip</param>
<param name="inputName">inputStreamZipByte</param>
<param name="contentDisposition">attachment;filename="myZip.zip"</param>
<param name="bufferSize">1024</param>
</result>
</action>发布于 2014-08-14 12:59:16
我找到了解决方案的一部分,但现在我有了另一个问题:
第一岗位的问题在于如何处理流。因为我在Zip和Workbook中使用了相同的outputStream。解决方案为每个工作簿创建了一个新的ByteArrayOutpuStream。
// Write WB conntent in outputStream
ByteArrayOutputStream wbOutputStream = new ByteArrayOutputStream();
wb.write(wbOutputStream);
addEntry(zip, myObject.getFileName(), wbOutputStream);
wbOutputStream.close();But...now生成的Zip文件已损坏.
发布于 2014-08-13 20:08:26
xlsx文件是一个zip文件。您应该检查适合结果的contentType参数的MIME类型。请参阅What is a correct mime type for docx, pptx etc?
发布于 2016-04-18 20:23:37
只需将工作簿写入BytearrayInputStream,然后转换为字节数组,使用ZipOutPutStream中的字节数组即可。
这个为我工作.
response.setContentType("application/zip"); Response.setHeader(“内容-处置”、“attachment;filename=\”“+fileName+”“.zip\”); workBook = getWorkbook(displayList,cfgType); ByteArrayOutputStream baos =新ByteArrayOutputStream(); ZipOutputStream zos =新ZipOutputStream(baos); ByteArrayOutputStream fileBaos = new >ByteArrayOutputStream(); Zos.putNextEntry(新ZipEntry("Test.xlsx")); workBook.write(fileBaos); zos.write(fileBaos.toByteArray()); 新的zos.putNextEntry(“Test1.xlsx”); zos.write(fileBaos.toByteArray()); fileBaos.close();zos.close(); response.getOutputStream().write(baos.toByteArray());response.flushBuffer();
https://stackoverflow.com/questions/25292034
复制相似问题