zoukankan      html  css  js  c++  java
  • Java web中不同浏览器间导出Excel文件名称乱码问题解决方案

    问题描述:

            对于不同浏览器存在对中文编码格式问题,从而在导出Excel文件时,中文文件名出现乱码的情况,即在程序中给要导出的文件指定一个中文名字时,在浏览器上出现的下载框中的文件名出现了乱码,解决如下:   

    解决方案:

    1.      Date dt=new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间  
    2.     DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//设置显示格式  
    3.     String nowTime="";  
    4.     nowTime= df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示  
    5.       
    6.     WritableWorkbook book = null;  
    7.      String fileName = null;  
    8.      fileName = "中文文件名"+nowTime+ ".xls";  
    9.      OutputStream os = null;           
    10.      final String userAgent = request.getHeader("USER-AGENT");  
    11.      try {  
    12.         os = response.getOutputStream();  
    13. esponse.reset();//清空输出流  
    14.   
    15.              String finalFileName = null;  
    16.              if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器  
    17.              finalFileName = URLEncoder.encode(fileName,"UTF8");  
    18.          }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器  
    19.              finalFileName = new String(fileName.getBytes(), "ISO8859-1");  
    20.          }else{  
    21.              finalFileName = URLEncoder.encode(fileName,"UTF8");//其他浏览器  
    22.          }  
    23.          response.setHeader("Content-Disposition", "attachment; filename="" + finalFileName + """);//这里设置一下让浏览器弹出下载提示框,而不是直接在浏览器中打开  
    24.          response.setContentType("application/vnd.ms-excel");  
    25.      } catch (UnsupportedEncodingException e) {  
    26.         e.printStackTrace();  
    27.      }catch (IOException e) {  
    28.        e.printStackTrace();  
    29. }  
    30.        
    31.         book = Workbook.createWorkbook(os);  
    32.              WritableSheet sheet = book.createSheet("Sheet_1", 0);.................................
  • 相关阅读:
    Javascript对象原型prototype和继承
    Javascript(js)使用function定义构造函数
    phpExcel中文帮助手册
    php curl_init函数用法
    nginx启动,重启,关闭命令
    Nginx配置文件详细说明
    Nginx 简单的负载均衡配置示例
    MySQL数据库的同步配置
    ucenter home 视频增加缩略图
    web分词程序和算法
  • 原文地址:https://www.cnblogs.com/lxl57610/p/7392646.html
Copyright © 2011-2022 走看看