上一節提到的兩種不同的方式,基本上在IE上都有不兼容的問題,這裏我們再來說說一種兼容兩者的方式,話不多說,直接上代碼
package com.action;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.util.ExcelUtil;
@Controller
public class ExcelAction {
// @RequestMapping("/download.do")
// public ResponseEntity download(HttpServletRequest request,HttpServletResponse response) throws Exception{
//
// byte[] content = new ExcelUtil().getExcelFileByte();
//
// HttpHeaders headers = new HttpHeaders();
// if(content == null || content.length == 0){
// headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
// return new ResponseEntity(null, headers, HttpStatus.OK);
// }
// headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
// headers.setContentDispositionFormData("attachment",new String("測試Excel.xls".getBytes("utf-8"),"ISO-8859-1"));
// return new ResponseEntity(content, headers, HttpStatus.CREATED);
// }
@RequestMapping("/download.do")
public void download(HttpServletRequest request,HttpServletResponse response) throws Exception{
OutputStream outputStream = null;
try{
byte[] content = new ExcelUtil().getExcelFileByte();
String fileName = "測試Excel.xls";
response.setContentType("application/octet-stream;charset=UTF-8");
String userAgent = request.getHeader("user-agent").toLowerCase();
if(userAgent != null &&((userAgent.indexOf("msie")!=-1) || (userAgent.indexOf("rv") != -1 && userAgent.indexOf("firefox") == -1))
&& ((userAgent.indexOf("msie") != -1 ||(userAgent.indexOf("rv")!=-1 && userAgent.indexOf("chrom") == -1)))){
//識別IE瀏覽器
fileName = URLEncoder.encode(fileName, "UTF-8");
} else {
//非IE瀏覽器
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
}
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.addHeader("Content-Length", String.valueOf(content.length));
outputStream = new BufferedOutputStream(response.getOutputStream());
outputStream.write(content);
outputStream.flush();
}catch(Exception e){
response.reset();
response.setContentType("text/html;charset=UTF-8");
try {
PrintWriter writer = response.getWriter();
writer.write("<script>");
writer.write("alert('導出數據發生異常,請聯繫客服!');");
writer.write("</script>");
writer.close();
} catch (IOException e1) {
e.printStackTrace();
}
}finally{
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
byte[] content = new ExcelUtil().getExcelFileByte(); 參考第一章
前端調用代碼如下
<!DOCTYPE html>
<html>
<head>
<title>我的測試</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<a id="download1">下載excel文檔</a>
<br>
<a id="download2">下載Excel文檔2</a>
</body>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript">
$("#download1").click(function(){
var contextHref = getContextPath();
window.location.href = contextHref+"/download.do";
});
function getContextPath(){
var pathName = document.location.pathname;
var index = pathName.substr(1).indexOf("/");
var contextRoot = pathName.substr(0,index+1);
var port = window.location.port;
return "http://localhost:"+port+contextRoot;
}
$("#download2").click(function(){
var contextHref = getContextPath();
var url = contextHref+"/download.do";
window.open(url, "_blank");
});
</script>
</html>
測試在IE和google上通過
以上解決方案,雖不是最好的解決方案,在網上大家也都能搜索到,希望對初學者有用,自己也當做一個筆記。如果有其他的解決方案,希望大家指點指點
另外給一些SpringExcel下載的URL給大家做參考
http://blog.csdn.net/qiubabin/article/details/50113675
http://blog.csdn.net/java_wisely/article/details/9765555
http://www.cnblogs.com/blog5277/p/6065601.html