对于xlsx的响应参数
response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName,"UTF-8") + ".xlsx"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
对于xls response.setContentType("application/vnd.ms-excel");
前端:
const blob = new Blob([res], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'}); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = '工单信息.xls'; a.click(); window.URL.revokeObjectURL(url);
正常情况下这样就行了,但是这样得到的excel文件
这种情况下我以为是乱码,但是不管前后端怎样调整,都是这个结果,我是直接去项目中导出的模板,并没有进行对象的转换,直接借助IOUtils工具类用response输出的
解决方案
前端直接window.location.href 方式去访问后端接口,因为后端接口会对前端的请求进行权限验证,把这个接口加上去就行
虽然完成了功能,但实际上并没有解决问题,如果有知道解决问题的办法的大佬,请指教下哈