2017-07-25 7 views
0

私はExcelワークブックは、ファイル作成せずに、次のように直接応答オブジェクトへのApache POIを使用して作成書いているでzip形式のXLSXとして:ダウンロードエクセル代わりにScalatra

val outputStream: ByteArrayOutputStream = new ByteArrayOutputStream() 
workbook.write(outputStream) 
ExcelOk(response.getOutputStream.write(outputStream.toByteArray)) 

をしかしサイズいったん応答が8kBを超えると、Chromeにzipというファイルが、FireFoxにはoctet-streamというファイルがダウンロードされ始めます。

マイExcelOkオブジェクトは次のようになります。

object ExcelOk { 
    def apply(body: Any = Unit, headers: Map[String, String] = ExcelContentType, reason: String = "") = { 
    halt(ActionResult(responseStatus(200, reason), body, headers)) 
    } 
} 

と私のExcelContentType(すなわち、レスポンスヘッダー)は以下の通りです:

val ExcelContentType = Map(
    "Access-Control-Allow-Credentials" -> "true", 
    "Access-Control-Allow-Methods" -> "GET, PUT, POST, DELETE, OPTIONS", 
    "Access-Control-Allow-Origin" -> "*", 
    "Access-Control-Max-Age" -> "1728000", 
    "Content-type" -> "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
    "Content-disposition" -> "attachment; filename=excel_report.xlsx" 
) 

私もヘッダリストに"Transfer-Encoding" -> "chunked"を追加しようとしたが、それ動作しません。

私も私のweb.xmlファイルにこのスニペットを追加したが、それはどちらか助けていない:

<mime-mapping> 
    <extension>xlsx</extension> 
    <mime-type>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet</mime-type> 
</mime-mapping> 

このに関するすべてのヘルプは有用であろう。この動作は、応答サイズが一定のしきい値を超えた後にのみ発生することに注意してください。

答えて

1

応答出力ストリームにコンテンツを書き込む前に応答ヘッダーを設定する必要があります。

response.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 
response.setHeader("Content-disposition", "attachment; filename=excel_report.xlsx") 

workbook.write(response.getOutputStream)