且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

难以从 Servlet 发送 Excel 工作簿

更新时间:2023-12-04 14:45:19

我使用的是一个非常旧的代码库,公司的政策是如果它没有坏就不要修复它",所以这里是我们对 XLSX 导出的 servlet 响应的方式看起来.

I work with a really old codebase and the company's policy is "If it aint broke don't fix it", so here's how our servlet response for XLSX Export looks.

response.setHeader("Expires", "0");
response.setHeader("Content-disposition", "attachment;filename=" + exportTitle + "_" + fileDate + ".xlsx"); // I noticed you had "/" before and after the filename, try removing that, and add the extension.
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // Our legacy code actually has "application/xlsx" and it works fine - but [other answers indicate better](https://***.com/questions/974079/setting-mime-type-for-excel-document)

...对于你的 AJAX,我会尝试删除 dataType 规范,因为你的响应头定义了它,jQuery 应该滚动"它.我相信 mimeType 和 contentType 用于定义发送到服务器的内容(您似乎已经表明它工作正常),所以我不会过多关注这一点.

... For your AJAX I would try removing dataType specification, since your response header defines it, jQuery should just "roll" with it. I believe the mimeType and contentType are used to define what's sent to the server (which you've seemed to have indicated is working fine), so I wouldn't focus too much on that.