Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 3664|回复: 0

JSON导出CSV中文乱码解决方案

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-4-13 20:20:24 | 显示全部楼层 |阅读模式

    前言

          以往datagrid导出数据全部在后台搞定,现在就想换中思路去解决,正常情况下使用easyui datagrid ajax获取数据源时都是json格式,那么此时需要导出数据时只要把该数据源扔出来直接导出CSV即可。

     

    中文乱码

         导出CSV后,字母和数字正常,中文成了乱码,一番google发现,有人提出用BOM的方式解决,主要是在导出路径添加后缀 \uFEFF

     

    导出方法

     

     1 function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
     2             //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
     3             var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData;
     4 
     5             var CSV = '';
     6             //Set Report title in first row or line
     7 
     8             CSV += ReportTitle + '\r\n\n';
     9 
    10             //This condition will generate the Label/Header
    11             if (ShowLabel) {
    12                 var row = "";
    13 
    14                 //This loop will extract the label from 1st index of on array
    15                 for (var index in arrData[0]) {
    16 
    17                     //Now convert each value to string and comma-seprated
    18                     row += index + ',';
    19                 }
    20 
    21                 row = row.slice(0, -1);
    22 
    23                 //append Label row with line break
    24                 CSV += row + '\r\n';
    25             }
    26 
    27             //1st loop is to extract each row
    28             for (var i = 0; i < arrData.length; i++) {
    29                 var row = "";
    30 
    31                 //2nd loop will extract each column and convert it in string comma-seprated
    32                 for (var index in arrData) {
    33                     row += '"' + arrData[index] + '",';
    34                 }
    35 
    36                 row.slice(0, row.length - 1);
    37 
    38                 //add a line break after each row
    39                 CSV += row + '\r\n';
    40             }
    41 
    42             if (CSV == '') {
    43                 alert("Invalid data");
    44                 return;
    45             }
    46 
    47             //Generate a file name
    48             var fileName = "";
    49             //this will remove the blank-spaces from the title and replace it with an underscore
    50             fileName += ReportTitle.replace(/ /g, "_");
    51 
    52             //Initialize file format you want csv or xls
    53             var uri = 'data:text/csv;charset=utf-8,\uFEFF' + encodeURI(CSV);
    54 
    55             // Now the little tricky part.
    56             // you can use either>> window.open(uri);
    57             // but this will not work in some browsers
    58             // or you will not get the correct file extension    
    59 
    60             //this trick will generate a temp <a /> tag
    61             var link = document.createElement("a");
    62             link.href = uri;
    63 
    64             //set the visibility hidden so it will not effect on your web-layout
    65             link.style = "visibility:hidden";
    66             link.download = fileName + ".csv";
    67 
    68             //this part will append the anchor tag and remove it after automatic click
    69             document.body.appendChild(link);
    70             link.click();
    71             document.body.removeChild(link);
    72         }

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-3 13:38 , Processed in 0.071327 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表