POI导出excel表时文件名变成乱码怎么办
1个回答
2017-06-20
展开全部
在用POI进行excel表导出时,遇到中文文件名乱码问题,用下面的方法得到了解决。
转载自:https://my.oschina.NET/chinamummy29/blog/525639
在导出前对名称根据浏览器做下处理
[java] view plain copy
<code class="hljs typescript" style=""><span class="hljs-comment" style="">// 判断浏览器类型,firefox浏览器做特殊处理,否则下载文件名乱码</span>
<span class="hljs-keyword" style="">public</span> <span class="hljs-keyword" style="">static</span> <span class="hljs-built_in" style="">void</span> compatibleFileName(HttpServletRequest request, HttpServletResponse response, <span class="hljs-built_in" style="">String</span> excelname) throws UnsupportedEncodingException {
<span class="hljs-built_in" style="">String</span> agent = request.getHeader(<span class="hljs-string" style="">"USER-AGENT"</span>).toLowerCase();
response.setContentType(<span class="hljs-string" style="">"application/vnd.ms-excel"</span>);
<span class="hljs-built_in" style="">String</span> fileName = excelname;
<span class="hljs-built_in" style="">String</span> codedFileName = java.net.URLEncoder.encode(fileName, <span class="hljs-string" style="">"UTF-8"</span>);
<span class="hljs-keyword" style="">if</span> (agent.contains(<span class="hljs-string" style="">"firefox"</span>)) {
response.setCharacterEncoding(<span class="hljs-string" style="">"utf-8"</span>);
response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + <span class="hljs-keyword" style="">new</span> <span class="hljs-built_in" style="">String</span>(fileName.getBytes(), <span class="hljs-string" style="">"ISO8859-1"</span>) + <span class="hljs-string" style="">".xls"</span>);
} <span class="hljs-keyword" style="">else</span> {
response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + codedFileName + <span class="hljs-string" style="">".xls"</span>);
}
}</code>
转载自:https://my.oschina.NET/chinamummy29/blog/525639
在导出前对名称根据浏览器做下处理
[java] view plain copy
<code class="hljs typescript" style=""><span class="hljs-comment" style="">// 判断浏览器类型,firefox浏览器做特殊处理,否则下载文件名乱码</span>
<span class="hljs-keyword" style="">public</span> <span class="hljs-keyword" style="">static</span> <span class="hljs-built_in" style="">void</span> compatibleFileName(HttpServletRequest request, HttpServletResponse response, <span class="hljs-built_in" style="">String</span> excelname) throws UnsupportedEncodingException {
<span class="hljs-built_in" style="">String</span> agent = request.getHeader(<span class="hljs-string" style="">"USER-AGENT"</span>).toLowerCase();
response.setContentType(<span class="hljs-string" style="">"application/vnd.ms-excel"</span>);
<span class="hljs-built_in" style="">String</span> fileName = excelname;
<span class="hljs-built_in" style="">String</span> codedFileName = java.net.URLEncoder.encode(fileName, <span class="hljs-string" style="">"UTF-8"</span>);
<span class="hljs-keyword" style="">if</span> (agent.contains(<span class="hljs-string" style="">"firefox"</span>)) {
response.setCharacterEncoding(<span class="hljs-string" style="">"utf-8"</span>);
response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + <span class="hljs-keyword" style="">new</span> <span class="hljs-built_in" style="">String</span>(fileName.getBytes(), <span class="hljs-string" style="">"ISO8859-1"</span>) + <span class="hljs-string" style="">".xls"</span>);
} <span class="hljs-keyword" style="">else</span> {
response.setHeader(<span class="hljs-string" style="">"content-disposition"</span>, <span class="hljs-string" style="">"attachment;filename="</span> + codedFileName + <span class="hljs-string" style="">".xls"</span>);
}
}</code>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询