POI--JAVA中把数据导出EXCEl

其实发现也没什么好说的。


只是注意如果这句话弹不下载的提示,除了要设置response之外

response.setCharacterEncoding("utf-8");

response.setContentType("multipart/form-data");

response.addHeader("Content-Disposition",

"attachment;filename="+strName);

还需要注意请求的方式,如果用jQuery的ajax请求是不行的,网上解释的大概意思应该是ajax不能正确传入response对象这样的,所以发起请求应该用<a>标签,或者<form>的action属性吧。

StringBuffer fileName = new StringBuffer(year+"年"+month+"月归档情况统计.xls");

String strName = null;

//处理浏览器不同下载的文件名乱码问题

try {

String agent = request.getHeader("User-Agent");

boolean isMSIE = ((agent != null && agent.indexOf("MSIE") != -1 ) || ( null != agent && -1 != agent.indexOf("like Gecko")));

if (isMSIE) {

strName = URLEncoder.encode(fileName.toString(), "UTF-8");

}

else {

strName = new String(fileName.toString().getBytes("UTF-8"),"ISO8859-1");

}

} catch (Exception e) {

e.printStackTrace();

}

这段代码也超级有用,下载你会发现文件名不能用中文,这时候就需要上面这段代码了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容