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

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时出现文件名乱码解决方法的详细内容,更多请关注其它相关文章!