簡體   English   中英

導出到 ServletOutputStream 的 Excel 文件已損壞

[英]Excel file exported to ServletOutputStream is corrupted

我正在將 excel 文件寫入 ServletOutputStream(對 xls 使用 HSSFWorkBook,對 xlsx 使用 XSSFWorkBook)。 作為 ServletResponse 的一部分下載的 excel 已損壞並包含垃圾字符。

  outStream = response.getOutputStream(); //ServletOutputStream outStream
  workbook.write(outStream); //HSSFWorkBook/XSSFWorkBook
  workbook.close();
  outStream.flush();

我嘗試將 excel 文件寫入 FileOutputStream 並且效果很好。 excel 文件可讀且完整。

在同一問題上檢查了其他幾個 StackOverflow 查詢。 曾嘗試將 servlet 響應的內容類型更改為“application/excel”、“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”、“application/vnd.ms-excel”等。但問題仍然存在。

However, I need to write the excel to ServletOutputStream (aware of the binary nature of the servlet output stream and had also tried writing excel to ByteOutputStream and then writing from the derived ByteOuputArray to ServletOutputStream).

請幫忙。

PS:此代碼已從 JSP 和 Java Servlet class 完成。 兩者都產生相同的結果。

非常感謝 BalusC!

我已按照建議重置 HttpServletResponse object,然后設置 header 並將 excel 寫入 ServletOutputStream。

response.reset()

excel下載完美!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM