$.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. border-radius的兼容问题

    border-radius可以便捷的制作圆角效果,不需要通过增加圆角图片来达到效果,便于维护. 语法:border-radius:none | 1-4 length | % / 1-4 length| ...

  2. Js控制Div在浏览器中的高度

    //需求是,我需要通过浏览器内容可视化的高度来控制div的高度boxheight(); //执行函数function boxheight(){ //函数:获取尺寸 //获取浏览器窗口高度 var wi ...

  3. Python写一个Windows下的android设备截图工具

    界面版 利用python的wx库写个ui界面,用来把android设备的截图输出到电脑屏幕,前提需要安装adb,涉及到的python库也要安装.代码如下: import wx,subprocess,o ...

  4. 注释声明:TODO HACK XXX FIXME REVIEW

    注释有时候也可以用来给一段代码声明额外的信息.这些声明的格式以单个单词打头并紧跟一个冒号.可以使用的声明如下. TODO: 说明代码还未完成.应当包含下一步要做的事情. HACK: 表明代码实现走了一 ...

  5. Excel图表的基本类型与选择

    图表类型分成4大类:1. 比较 2. 分布 3. 构成 4. 联系 转载自:https://zhuanlan.zhihu.com/peter-zhang-jie

  6. C++Builder XE8_upd1破解安装成功纪要

    1.先下载XE8,安装失败.2.后下载XE8U1(delphicbuilder_xe8_upd1_subscription.iso),安装,选择DelPhi.C++builder,输入系列号:3.安装 ...

  7. 关于 OJ1575的参考题解

    #include <stdio.h>int main(){ int a,b; scanf("%d",&a); b=0; while(a) { b+=a%10; ...

  8. C语言多文件编译

    fun.c #include <stdio.h> int fun1(){ printf("The first function!\n"); return 0; } in ...

  9. AOP 面向切面编程

    AOP http://blog.csdn.net/xiang_j2ee/article/details/6851963 Android 支持 AspectJ 这个库来实现面向切面编程. 使用 Apac ...

  10. USB传输协议。——Arvin

    问题一:USB的传输线结构是如何的呢? 答案一:一条USB的传输线分别由地线.电源线.D+.D-四条线构成,D+和D-是差分输入线,它使用的是3.3V的电压(注意哦,与CMOS的5V电平不同),而电源 ...