版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daijiguo/article/details/83301440
Html
<el-button @click=downloadExcel>数据导出</el-button>
export default {
methods:{
downloadExcel(){
window.location.href='/api/visitors/downloadExcel'
}
}
}
Java
@RequestMapping(value = "UserExcelDownloads", method = RequestMethod.GET)
public void downloadAllClassmate(HttpServletResponse response) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("信息表");
String fileName = "abc.xls";//设置要导出的文件的名字
String[] headers = { "学号", "姓名", "身份类型", "登录密码"};//headers表示excel表中第一行的表头
HSSFRow row = sheet.createRow(0);//在excel表中添加表头
for(int i=0;i<headers.length;i++){
HSSFCell cell = row.createCell(i);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
workbook.write(response.getOutputStream());
}
创建过程 workbook => sheet => row => cell,此方式如果需要传参,可以
/api/visitors/downloadExcel'?a=1&b=2
的方式传少量参数,因为get方式不支持大量数据传参。