溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么解決java導(dǎo)出excel時(shí)文件名亂碼

發(fā)布時(shí)間:2020-06-19 16:59:00 來(lái)源:億速云 閱讀:803 作者:元一 欄目:編程語(yǔ)言

java解決導(dǎo)出Excel時(shí)文件名亂碼的方法示例:

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() 方法以指定的編碼格式編碼字符串。

擴(kuò)展:

Java POI導(dǎo)出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導(dǎo)出excel時(shí)出現(xiàn)文件名亂碼解決方法的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注億速云其它相關(guān)文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI