java后台导出Excel文件

/**
  * @param workbook  excel工作簿
  * @param fileName  文件名
  * @param response  HttpServletResponse请求
  * @return 无
  */
private void downExcelFile(Workbook workbook, String fileName, HttpServletResponse response) {
		try {
			response.setHeader("Content-Disposition",
					"attachment;filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8"));
			response.setHeader("Content-Type", "application/x-download");
			response.setHeader("Access-Control-Allow-Origin", "*");
			// response.setContentType("application/octet-stream");
			// response.setHeader("Content-Length", len);//设置内容长度
			OutputStream os = response.getOutputStream();
			workbook.write(os);
			os.flush();
			os.close();
		} catch (Exception exp) {
			log.error("下载文件出现系统错误", exp);
		}
	}

将代码添加到自己要实现导出功能的controller中。注意该方法为私有方法。

你可能感兴趣的:(java工具类)