前言

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

中文乱码

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

导出方法

 function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {
//If JSONData is not an object then JSON.parse will parse the JSON string in an Object
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData; var CSV = '';
//Set Report title in first row or line CSV += ReportTitle + '\r\n\n'; //This condition will generate the Label/Header
if (ShowLabel) {
var row = ""; //This loop will extract the label from 1st index of on array
for (var index in arrData[0]) { //Now convert each value to string and comma-seprated
row += index + ',';
} row = row.slice(0, -1); //append Label row with line break
CSV += row + '\r\n';
} //1st loop is to extract each row
for (var i = 0; i < arrData.length; i++) {
var row = ""; //2nd loop will extract each column and convert it in string comma-seprated
for (var index in arrData[i]) {
row += '"' + arrData[i][index] + '",';
} row.slice(0, row.length - 1); //add a line break after each row
CSV += row + '\r\n';
} if (CSV == '') {
alert("Invalid data");
return;
} //Generate a file name
var fileName = "";
//this will remove the blank-spaces from the title and replace it with an underscore
fileName += ReportTitle.replace(/ /g, "_"); //Initialize file format you want csv or xls
var uri = 'data:text/csv;charset=utf-8,\uFEFF' + encodeURI(CSV); // Now the little tricky part.
// you can use either>> window.open(uri);
// but this will not work in some browsers
// or you will not get the correct file extension //this trick will generate a temp <a /> tag
var link = document.createElement("a");
link.href = uri; //set the visibility hidden so it will not effect on your web-layout
link.style = "visibility:hidden";
link.download = fileName + ".csv"; //this part will append the anchor tag and remove it after automatic click
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

JSON导出CSV中文乱码解决方案的更多相关文章

  1. Mysql 导入导出csv 中文乱码

    这篇文章介绍了Mysql 导入导出csv 中文乱码问题的解决方法,有需要的朋友可以参考一下   导入csv: load data infile '/test.csv' into table table ...

  2. mongoexport导出csv中文乱码

    在用mongoexport导出csv文件时,发现数据库中的中文在excel中都显示为乱码,用notepad打开则正常. 解决办法: 在notepad中,将编码格式改为UTF-8,保存,再用excel打 ...

  3. Django导出excel中文乱码解决方案

    Django官方文档有关于怎么生成csv文件的方法 import csv from django.http import HttpResponse def some_view(request): # ...

  4. AJAX中文乱码解决方案

    通过AJAX获取数据中文乱码解决方案: @ResponseBody 作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到 ...

  5. aspx页面,中文乱码解决方案

    由于文件编码方式编码方式不统一出现样式中文乱码解决方案: 今天碰到的问题:页面字体样式设置的'微软雅黑',可页面没引用.我调试看到样式出现中文乱码了 这种问题,就需要转换文件的编码方式,如下两步即可解 ...

  6. JSP中pageEncoding和charset区别,中文乱码解决方案(转载)

    转载自:JSP中pageEncoding和charset区别,中文乱码解决方案 JSP指令标签中<%@ page contentType="text/html;charset=GB23 ...

  7. boost::xml——基本操作以及中文乱码解决方案 (续)

    本博文主要想说明以下两点: 1.对于上一篇的<boost::xml——基本操作以及中文乱码解决方案>解释,这篇博文基本解决了正确输入输出中英文问题,但是好像还没有解决修改中文出现乱码的问题 ...

  8. [转]mysql导入导出数据中文乱码解决方法小结

    本文章总结了mysql导入导出数据中文乱码解决方法,出现中文乱码一般情况是导入导入时编码的设置问题,我们只要把编码调整一致即可解决此方法,下面是搜索到的一些方法总结,方便需要的朋友. linux系统中 ...

  9. 基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案

    基于Windows环境下cmd/编译器无法输入中文,显示中文乱码解决方案 两个月前做C++课设的时候,电脑编译器编译结果出现了中文乱码,寻求了百度和大神们,都没有解决这个问题,百度上一堆解释是对编译器 ...

随机推荐

  1. toroiseSVN 无法连接服务器,提示unable connect to ……url 参数错误

    之前使用的好好的,有天突然提示无法连接repository url,能ping通服务器,就是一直报错,找了很多方法,如: 1.删除缓存及缓存文件 2.删除软件并重新安装 3.关闭windows防火墙 ...

  2. BSBuDeJie_05

    1 点包装成对象 [NSValue valueWithCGPoint] 2 获取当前时刻 CACurrentMediaTime

  3. front end about mobile web techs

    WEB OF DEVICES http://www.w3.org/standards/webofdevices/ MOBILE WEB http://www.w3.org/standards/webd ...

  4. 一篇关于匿名函数(function(){})()不错的文章

    代码如下: (function(){ //这里忽略jQuery所有实现 })(); (function(){ //这里忽略jQuery所有实现 })(); 半年前初次接触jQuery的时候,我也像其他 ...

  5. ASP.NET Core 数据保护(Data Protection)【中】

    前言 上篇主要是对 ASP.NET Core 的 Data Protection 做了一个简单的介绍,本篇主要是介绍一下API及使用方法. API 接口 ASP.NET Core Data Prote ...

  6. ASP.NET Core中使用URL重写

    ASP.NET Core 1.1 Preview 1 中新增了 URL Rewriting middleware ,终于可以进行 URL 重写了,实际使用体验一下. 首先要将 ASP.NET Core ...

  7. jQuery.Callbacks之demo

    jQuery.Callbacks是jquery在1.7版本之后加入的,是从1.6版中的_Deferred对象中抽离的,主要用来进行函数队列的add.remove.fire.lock等操作,并提供onc ...

  8. 仅此一文让你明白ASP.NET MVC 之Model的呈现(仅此一文系列三)

    本文目的 我们来看一个小例子,在一个ASP.NET MVC项目中创建一个控制器Home,只有一个Index: public class HomeController : Controller { pu ...

  9. 源文件移动后gdb不显示代码的原因

    源文件移动后gdb不显示代码的原因   问题 我们从一个最简单的C语言程序开始.源文件main.c在 用户目录gdb文件夹下. florian@florian-pc:~/gdb$ cat main.c ...

  10. Linux RAID卡优化

    200 ? "200px" : this.width)!important;} --> 介绍 我们的生产服务器经常会做raid存储,但是单单做了raid就能保证性能高效和数据 ...