我正在 ServletOutputStream 上寫一個 excel 檔案(使用 HSSFWorkBook for xls 和 XSSFWorkBook for xlsx)。作為 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”等。但問題仍然存在。
但是,我需要將 excel 寫入 ServletOutputStream(了解 servlet 輸出流的二進制性質,并且還嘗試將 excel 寫入 ByteOutputStream,然后從派生的 ByteOuputArray 寫入 ServletOutputStream)。
請幫忙。
PS:此代碼已從 JSP 和 Java Servlet 類完成。兩者產生相同的結果。
uj5u.com熱心網友回復:
非常感謝 BalusC!
我已按照建議重置 HttpServletResponse 物件,然后設定標頭并將 excel 寫入 ServletOutputStream。
response.reset()
excel下載完美!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/374124.html
標籤:爪哇 擅长 小服务程序 apache-poi xssf
上一篇:回圈遍歷ExcelPowerQuery中的Json串列以檢索記錄
下一篇:如何將視頻插入Oracle資料庫