只需要在jsp文件头上设置,其关键代码如下:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
response.setHeader("Pragma ", "public");
response.setHeader("Cache-Control ", "must-revalidate, post-check=0, pre-check=0 ");
response.addHeader("Cache-Control ", "public ");
response.addHeader("Content-Disposition", "attachment; filename=user.xls");
response.setContentType("application/vnd.ms-excel.numberformat:@;charset=UTF-8");
%>
<html>
注意添加相关依赖:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>