$.ajax({
type: 'GET',
async: false,
url: '../../api/screening/exportTable?seriesIndex=' + param.seriesIndex +'&dataIndex=' + param.dataIndex + '&package1=' + mode,
success: function(data) {
JSONToCSVConvertor(JSON.stringify(data), title, true);
},
error: function(XMLHttpRequest) {
$.messager.show({msg:XMLHttpRequest});
}
})

网上抄的方法

var uri = 'data:text/csv;charset=utf-8,\uFEFF' + encodeURI(CSV);解决中文乱码

 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 = '';
//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);
}

后台生产JSONArray

 @RequestMapping(value = "/exportTable", method = RequestMethod.GET)
public @ResponseBody JSONArray exportTable(HttpServletRequest request,
HttpServletResponse response, String seriesIndex, String dataIndex, String package1) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map = getAnalyzingSelectedList(request, response, seriesIndex, dataIndex, null, package1);
Iterable<DmMeasureBean> list= (List<DmMeasureBean>)map.get("rows");
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
for(DmMeasureBean bean : list) {
jsonObject.clear();
jsonObject.put("**", bean.getName());
jsonObject.put("**", bean.getAge());
jsonObject.put("**", bean.getGenderString());
jsonObject.put("**", new SimpleDateFormat("yyyy-MM-dd").format(bean.getBirthday()));
jsonObject.put("**", bean.getResult() == null ? "" : bean.getResult());
jsonObject.put("**", bean.getExamDate() == null ? "" : new SimpleDateFormat("yyyy-MM-dd").format(bean.getExamDate()));
jsonObject.put("**", bean.getTel());
jsonArray.add(jsonObject);
}
return jsonArray;
}

Java Js实现导出csv的更多相关文章

  1. java实现接口导出csv文件

    Tomxin7 Simple, Interesting | 简单,有趣 业务介绍 项目要求从数据库中查询出相关数据后,通过表格展示给用户,如果用户需要,可以点击导出按钮,导出数据为csv格式. 开发环 ...

  2. Java - 问题集 - 导出csv文件中文乱码

    微软的excel文件需要通过文件头的bom来识别编码,所以写文件时,需要先写入bom头. FileOutputStream fos = new FileOutputStream(new File(&q ...

  3. java utf8字符 导出csv 文件的乱码问题。

    在输出的格式为UTF-8的格式,但是打开CSV文件一直为乱码,后来参考了这里的代码,搞定了乱码问题,原文请参考:http://hbase.iteye.com/blog/1172200 private ...

  4. Web 端 js 导出csv文件(使用a标签)

    前言 导出文件,使用最多的方式还是服务器端来处理.比如jsp 中使用response 的方式. 但是,有时候可能就想使用web 前端是否也可以把页面上的内容导出来呢? 比如说,导出页面的一个表格. 这 ...

  5. java导出csv格式文件

    导出csv格式文件的本质是导出以逗号为分隔的文本数据 import java.io.BufferedWriter; import java.io.File; import java.io.FileIn ...

  6. Java 导出 CSV

    package com.cib.cap4j.cfn.util; import java.io.BufferedWriter; import java.io.File; import java.io.F ...

  7. 【转载】JS导出CSV文件

    转自:http://www.cnblogs.com/dengnan/p/3990211.html 通过自己实际测试有以下几种方法 方法一通过a标签实现,把要导出的数据用“\n”和“,”拼接成一个字符串 ...

  8. JAVA导出csv出现0.00E+00

    导出csv出现 0.00E+00的问题,打印其值为0E-8:这是因为数据表中无对应数据(decimal),查询结果则为 0e-8. 出现的字段是多个字段相加产生的和,所以这里调用了一个相加的方法.在相 ...

  9. springMVC导出 CSV案例

    导出csv 第一步 Controller类里调用 OrderParamsVo 传入的参数 orderService.findBuyCSV 查询到要导出的信息 /** * 购买订单CSV * Order ...

随机推荐

  1. 如何在RCP程序中添加一个banner栏

    前言:这段时间还算比较空闲,我准备把过去做过的有些形形色色,甚至有些奇怪的研究总结一下,也许刚好有人用的着也不一定,不枉为之抓耳挠腮的时光和浪费的电力.以前有个客户提出要在RCP程序中添加一个bann ...

  2. CSS 选择器

    1.  .   ==> class选择器 2.  #  ==> id选择器 3.  *  ==>选择所有元素 4.<p>  ==>选择所有<p>标签的元 ...

  3. FtpClient.storeFile返回false解决方法

    在确定路径和文件名没有中文的情况下添加以下代码 ftp.setFileTransferMode(ftp.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode();/ ...

  4. angular学习input输入框筛选

    学习angular,看到 angular-phonecat测试用例,照着教程运行了一遍,对于初学者有点不是很理解angular 帅选代码的意思,于是找教材,参考资料,明白了input筛选原来这么简单. ...

  5. 含有多个main方法的jar包的运行方式(适用于用java写的工具类)

    如果一个jar中含有多个主程序,而你没有配置默认主程序,或者想要运行指定主程序,则可以通过如下命令执行: Java -cp example03-1.0-SNAPSHOT.jar cn.vicky.ex ...

  6. java中的数据结构(集合|容器)

    对java中的数据结构做一个小小的个人总结,虽然还没有到研究透彻jdk源码的地步.首先.java中为何需要集合的出现?什么需求导致.我想对于面向对象来说,对象适用于描述任何事物,所以为了方便对于对象的 ...

  7. GNS3 IOU 配置

    GNS3使用视频: http://edu.51cto.com/lesson/id-25295.html GNS3 IOU 与VM http://www.mamicode.com/info-detail ...

  8. Javascript优化后的加减乘除(解决js浮点数计算bug)

    function add(a, b) { var c, d, e; try { c = a.toString().split(".")[1].length; } catch (f) ...

  9. 整理mac上的php环境

    重装了一下mb,重新编译环境太麻烦了,所以整理一下mac系统自带php环境用: OS X yosemite 10.10.5  自带apache,php 1,让apache启用php模块 sudo vi ...

  10. upupw一键绿色免安装环境包

    项目测试,选择upupw环境包 下载nginx版本,解压即可使用 任务就是要把我电脑上的项目test.com提供给公司局域网同事访问,如果是apache的话,前面的wampserver已经讲过了. 1 ...