try{
//捕获内存缓冲区的数据,转换为字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
workbook.write(out);
//获取内存缓冲中的数据
byte[] content = out.toByteArray();
//将字节数据转化为输入流
InputStream in = new ByteArrayInputStream(content);
//通过调用reset()方法可以重新定位
response.reset();
//JSONP 解决跨域问题
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods","GET,POST,PUT,DELETE,OPTIONS");
response.addHeader("Access-Control-Allow-Headers", "WWW-Authenticate,Authorization,Set-Cookie," +
"X-Requested-With,Accept-Version,Content-Length,Content-Type,Date,X-Api-Version,name");
response.addHeader("Access-Control-Allow-Credentials", "true");
// response.setContentType("application/octet-stream");
//如果文件名是英文名不需要加编码格式,如果是中文名需要添加"ios-8859-1"防止乱码
response.setHeader("Content-Disposition", "attachment;filename=" +
new String((fileName + ".xls").getBytes("gb2312"), "iso-8859-1"));
response.setHeader("Content-Length", "" + content.length);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] buff = new byte[8192];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))){
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.close();
outputStream.flush();
outputStream.close();
}catch (IOException ex){
ex.printStackTrace();
}