无论使用poi还是使用jxl导出excel都需要用到流
一种是outputstrean,另一种fileoutputstream
第一种:如果想要弹出保存的提示框必须加入下列三句
response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setHeader("Content-Disposition","attachment;filename="+filename);
response.setCharacterEncoding("utf-8");
OutputStream os=response.getOutputStream();
在使用第一种的时候,我用的ajax请求。导致excel无法导出,最后我直接请求可以导出(document.location.href="${pageContext.request.contextPath}/tran/export.do?")
原因是:ajax也用到了response.getWriter()方法 要将 数据结果回传,这里 我虽然 放弃了 回传的写入流writer 参数, 但是ajax还是会默认的去掉用,把流已经占用了,当然返回不了任何东西了。
第二种:
action中使用
FileOutputStream fos=new FileOutputStream(file);
此时可以使用ajax请求,在导出成功后返回文件路径,在页面中使用window.open(path);即可打开导出的excel文件
以下为生成Excel操作
1.生成Excel文件并添加数据,Action中:
添加响应事件,通过getExportPath获得工程路径,与jsp中获得request.getContextPath()效果相同,fileName为要下载的文件名,经过拼接filePath是xls文件的绝对路径,调用工具类DownLoadUtil中的downLoadUtil方法;
@RequestMapping("export.do")
public void export(QueryRequest QueryRequest, HttpServletRequest request,HttpServletResponse response){
try {
EPageResponse<TranInfo> ePageInfo = this.tranService.tranExport(QueryRequest, this.getTranComm());
String exportPath = this.getExportPath(request);
WindDate wd = new WindDate();
String fileName = wd.toYYYYMMDDHHMMSS()+ "缴费交易流水" +".xls";
String filePath = exportPath + File.separator + fileName;
WritableWorkbook book = Workbook.createWorkbook(new File(filePath));
WritableSheet sheet = book.createSheet("交易流水列表", 0);
this.setSheel(ePageInfo.getRows(),sheet);
book.write();
book.close();
DownloadUtil.downFile(filePath, request, response);
} catch (Exception e) {
e.printStackTrace();
String msg = e.getMessage();
if (null == msg)
msg = "其他异常 ";
}
}
public String getExportPath(HttpServletRequest request){
String path = (String) request.getSession().getServletContext().getInitParameter("EXPORT_PATH");
File file = new File(path);
if(!file.exists())file.mkdirs();
return path;
}
2.弹出下载框,工具类DownLoadUtil中:
public static void downFile(String filepath, HttpServletRequest request,HttpServletResponse response){
try{
File temFile = new File(filepath);
if(!temFile.exists()){
response.getWriter().write("ERROR:File Not Found");
return ;
}
String fileName = filepath.substring(filepath.lastIndexOf(File.separator)+1);
String browser = request.getHeader("user-agent");
Pattern p = Pattern.compile(".*MSIE.*?;.*");
Matcher m = p.matcher(browser);
if(m.matches()){
response.setHeader("Content-Disposition", "attachment; filename=" +URLEncoder.encode(fileName, "UTF-8").replace("+",""));
}else{
response.setHeader("Content-Disposition", "attachment; filename=" +new String(fileName.getBytes("UTF-8"),"ISO8859-1").replace(" ", ""));
}
response.setHeader("Cache-Control", "max-age=" + 100);
response.setContentLength((int) temFile.length());
response.setContentType("application/x-download");
response.setHeader("windows-Target", "_blank");
OutputStream ot=response.getOutputStream();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(temFile));
BufferedOutputStream bos = new BufferedOutputStream(ot);
byte[] buffer = new byte[4096];
int length = 0;
while((length = bis.read(buffer)) > 0){
bos.write(buffer,0,length);
}
bos.close();
bis.close();
ot.close();
} catch (Exception e) {
e.printStackTrace();
}
}