springmvc下載excel檔案,通過get方式傳少量引數
阿新 • • 發佈:2018-11-19
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方式不支援大量資料傳參。