赞
踩
1.post的方法里要加responseType: 'arraybuffer’参数,不然下载的excel会乱码
2.使用{type: “application/vnd.ms-excel”}的写法,可以保存为xls格式的excel文件(兼容老版本)。而使用“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”则会保存为xlsx
3.返回结果为下载excel文档链接,使用window.open(result)即可
4.使用增加节点调用click方法,而不使用window.open(objectUrl)方法,是防止被浏览器当插件屏蔽弹出连接
5.给文件设定名字,直接在a标签的download属性中设置即可
resultexport () { this.$http .post( '/votehoutai/export/resultexport', this.CEtableData, { headers: { 'content-type': 'application/json' }, // responseType: 'arraybuffer' } ).then((res) => { console.log(res) var blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) var objectUrl = URL.createObjectURL(blob) var a = document.createElement('a') document.body.appendChild(a) a.style = 'display: none' a.href = objectUrl a.click() document.body.removeChild(a) }) },
Controller:(ResultVo 是前端显示的Vo对象)
@PostMapping("resultexport")
@ApiOperation("导出")
@LogOperation("导出")
public void resultexport(@RequestBody List<ResultVo> obj, HttpServletResponse response) throws Exception {
ExcelUtils.exportExcelToTarget(response, null, obj, ResultExcel.class);
}
Excelutils:
public class ExcelUtils { /** * Excel导出 * * @param response response * @param fileName 文件名 * @param list 数据List * @param pojoClass 对象Class */ public static void exportExcel(HttpServletResponse response, String fileName, Collection<?> list, Class<?> pojoClass) throws IOException { if(StringUtils.isBlank(fileName)){ //当前日期 fileName = DateUtils.format(new Date()); } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, list); response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls"); ServletOutputStream out = response.getOutputStream(); workbook.write(out); out.flush(); } /** * Excel导出,先sourceList转换成List<targetClass>,再导出 * * @param response response * @param fileName 文件名 * @param sourceList 原数据List * @param targetClass 目标对象Class */ public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> sourceList, Class<?> targetClass) throws Exception { List targetList = new ArrayList<>(sourceList.size()); for(Object source : sourceList){ Object target = targetClass.newInstance(); BeanUtils.copyProperties(source, target); targetList.add(target); } exportExcel(response, fileName, targetList, targetClass); } }
ReesultExcel:
public class ResultExcel { @Excel(name="得票数") private int sum;//得到的票数和 @Excel(name="排名") private int rank;//排名 @Excel(name="姓名") private String name; @Excel(name="院系") private String faculty;//院系 @Excel(name="职位") private String position;//职位 @Excel(name="竞争职位") private String wantposition;//竞争的职位 @Excel(name="推荐类型") private int recommendtype;//推荐类型 }
ResultVo:
public class ResultVo extends BaseEntity {
private int cuserid;
private int sum;//得到的票数和
private int rank;//排名
private String name;
private String faculty;//院系
private String position;//职位
private String wantposition;//竞争的职位
private int recommendtype;//推荐类型
private Boolean group;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。