• 导出excel,后端返回文档流。前后端代码


    后端代码,方便查看,放在一起了

    /**
    	 * 导出报名表
    	 */
    	@Log("导出报名表")
    	@GetMapping( "/export")
    	@ResponseBody
    	@RequiresPermissions("platform:activity:export")
    	public void export(HttpServletResponse response,@RequestParam("id") Integer id) throws Exception {
    		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    		List<ActivityEnrollInfo> list = activityService.listEnrollInfo(id);
    		if (list == null || list.get(0).getEnrollNum() == 0) {
    			throw new Exception("报名人数为0,无法导出!");
    		}
    		response.reset();
    		response.setCharacterEncoding("utf-8");
    		String fileName = list.get(0).getActivityName()+"报名信息";
    		XSSFWorkbook wb = new XSSFWorkbook();
    		XSSFSheet sheet = wb.createSheet(fileName);
    		XSSFRow row = null;
    		row = sheet.createRow(0);//创建第一个单元格
    		row.setHeight((short) (400));
    
    		row.createCell(0).setCellValue("活动名称");
    		row.createCell(1).setCellValue("报名人数");
    		row.createCell(2).setCellValue("学员姓名");
    		row.createCell(3).setCellValue("城市");
    		row.createCell(4).setCellValue("手机号");
    		row.createCell(5).setCellValue("邮箱");
    		row.createCell(6).setCellValue("备注");
    
    		for (int i = 0; i < list.size(); i++) {
    			row = sheet.createRow(i + 1);
    			ActivityEnrollInfo devmonitorData = list.get(i);
    			row.createCell(0).setCellValue(devmonitorData.getActivityName());
    			row.createCell(1).setCellValue(devmonitorData.getEnrollNum());
    			row.createCell(2).setCellValue(devmonitorData.getName());
    			row.createCell(3).setCellValue(devmonitorData.getCity());
    			row.createCell(4).setCellValue(devmonitorData.getMobile());
    			row.createCell(5).setCellValue(devmonitorData.getEmail());
    		}
    		sheet.setDefaultRowHeight((short) (400));
    		//列宽自适应
    		for (int i = 0; i <= 13; i++) {
    			sheet.autoSizeColumn(i);
    		}
    		response.setContentType("application/vnd.ms-excel;charset=utf-8");
    		OutputStream os = null;
    		try {
    			os = response.getOutputStream();
    			response.addHeader("Content-disposition", "attachment;filename=" + fileName + "-" + simpleDateFormat.format(new Date()) + ".xlsx");//默认Excel名称
    			wb.write(os);
    			os.flush();
    			os.close();
    			return;
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		return;
    	}
    

      

    前端代码

    因为是返回文档流,采用window.open(urls); 的方式下载excel。注意这种方式需要后台是支持get请求

  • 相关阅读:
    [Sql Server][原创]
    SQL Server T-SQL高级查询
    SQL 网文链接
    Epicor系统二次开发
    lambda表达式的变量作用域
    写一个正则表达式匹配手机号
    函数装饰器在类方法中的使用方法
    关于Django的session的使用 (装饰器版)
    Django ORM相关操作(2)
    Django ORM相关操作(1)
  • 原文地址:https://www.cnblogs.com/onlyzhangmeng/p/16415459.html
Copyright © 2020-2023  润新知