怎么解决java导出excel时文件名乱码
发布时间:2020-06-19 16:59:00
来源:亿速云
阅读:137
作者:元一
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:
org.apache.poi
poi
3.14
org.apache.poi
poi-ooxml
3.14
org.apache.poi
poi-ooxml-schemas
3.14
org.apache.poi
poi-scratchpad
3.14
Controller:
@Transactional(propagation=Propagation.SUPPORTS)
@ResponseBody
@RequestMapping("/ExportInterpretationSignsWord")
public ResponseEntity 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 temp=buildService.ExportInterpretationSignsWordById(id);
return temp;
}