温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么解决java导出excel时文件名乱码

发布时间:2020-06-19 16:59:00 来源:亿速云 阅读:786 作者:元一 栏目:编程语言

java解决导出Excel时文件名乱码的方法示例:

String agent = request.getHeader("USER-AGENT").toLowerCase();
   response.setContentType("application/vnd.ms-excel");
   String fileName = "文件名";
   String codedFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
   if (agent.contains("firefox")) {
           response.setCharacterEncoding("utf-8");
           response.setHeader("content-disposition", "attachment;filename=" + new String(fileName.getBytes(), "ISO8859-1") + ".xls" );
  } else {
          response.setHeader("content-disposition", "attachment;filename=" + codedFileName + ".xls");
   }

encode() 方法以指定的编码格式编码字符串。

扩展:

Java POI导出Word文档代码

pom.xml:

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.14</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.14</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml-schemas</artifactId>

<version>3.14</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-scratchpad</artifactId>

<version>3.14</version>

</dependency>


Controller:

@Transactional(propagation=Propagation.SUPPORTS)

@ResponseBody

@RequestMapping("/ExportInterpretationSignsWord")

public  ResponseEntity<byte[]>  ExportInterpretationSignsWord(@RequestParam String token,HttpServletRequest request,

HttpServletResponse response,

@RequestParam int id) throws IOException{

response.setContentType("text/html;charset=UTF-8");   

    response.setHeader("Content-type", "application/json;charset=UTF-8");

    if(RedisPool.checkToken(token)==false) {

    return null;

    }

ResponseEntity<byte[]> temp=buildService.ExportInterpretationSignsWordById(id);

return temp; 

}

以上就是java导出excel时出现文件名乱码解决方法的详细内容,更多请关注亿速云其它相关文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI