结论:在jsp中加上out.clear即可(前提保证生成的excel在服务器上是正确的,只是浏览器传输才出现乱码)。
dowload.jsp完整代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.userpackage.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request. setCharacterEncoding("UTF-8");
//response.setHeader("Content-Type", "charset=UTF-8");
%>
<%
/*
Enumeration pNames = request.getParameterNames();
String keyStr="";
String valueStr="";
Map map = new HashMap();
while (pNames.hasMoreElements()) {
keyStr = pNames.nextElement().toString();
valueStr = request.getParameter(keyStr).toString().trim();
if(!"".equals(valueStr)){
out.println(keyStr+"=="+valueStr);
}
}
*/
out.clear();
out = pageContext.pushBody();
ExcelExport.export(request,response);
out.println("22222");
%>
现象:
response.reset();// 在servlet中加入,清除首部的空白行