Java Js实现导出csv
$.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的更多相关文章
- java实现接口导出csv文件
Tomxin7 Simple, Interesting | 简单,有趣 业务介绍 项目要求从数据库中查询出相关数据后,通过表格展示给用户,如果用户需要,可以点击导出按钮,导出数据为csv格式. 开发环 ...
- Java - 问题集 - 导出csv文件中文乱码
微软的excel文件需要通过文件头的bom来识别编码,所以写文件时,需要先写入bom头. FileOutputStream fos = new FileOutputStream(new File(&q ...
- java utf8字符 导出csv 文件的乱码问题。
在输出的格式为UTF-8的格式,但是打开CSV文件一直为乱码,后来参考了这里的代码,搞定了乱码问题,原文请参考:http://hbase.iteye.com/blog/1172200 private ...
- Web 端 js 导出csv文件(使用a标签)
前言 导出文件,使用最多的方式还是服务器端来处理.比如jsp 中使用response 的方式. 但是,有时候可能就想使用web 前端是否也可以把页面上的内容导出来呢? 比如说,导出页面的一个表格. 这 ...
- java导出csv格式文件
导出csv格式文件的本质是导出以逗号为分隔的文本数据 import java.io.BufferedWriter; import java.io.File; import java.io.FileIn ...
- Java 导出 CSV
package com.cib.cap4j.cfn.util; import java.io.BufferedWriter; import java.io.File; import java.io.F ...
- 【转载】JS导出CSV文件
转自:http://www.cnblogs.com/dengnan/p/3990211.html 通过自己实际测试有以下几种方法 方法一通过a标签实现,把要导出的数据用“\n”和“,”拼接成一个字符串 ...
- JAVA导出csv出现0.00E+00
导出csv出现 0.00E+00的问题,打印其值为0E-8:这是因为数据表中无对应数据(decimal),查询结果则为 0e-8. 出现的字段是多个字段相加产生的和,所以这里调用了一个相加的方法.在相 ...
- springMVC导出 CSV案例
导出csv 第一步 Controller类里调用 OrderParamsVo 传入的参数 orderService.findBuyCSV 查询到要导出的信息 /** * 购买订单CSV * Order ...
随机推荐
- [OSG][osgEarth]osgEarth例子程序简介
1.osgearth_graticule:生成经纬线. 2.osgearth_annotation:各类标注(点.线.面.模型.文本等). 3.osgearth_city:加载一个城市三维模型,可以浏 ...
- Java8闭包
闭包在很多语言中都存在,例如C++,C#.闭包允许我们创建函数指针,并把它们作为参数传递,Java编程语言提供了接口的概念,接口中可以定义抽象方法,接口定义了API,并希望用户或者供应商来实现这些方法 ...
- Python 对目录中的文件进行批量转码(GBK>UTF8)
通过python实现对文件转码,其实处理很简单: 1.打开读取文件内容到一个字符串变量中,把gbk编码文件,对字符串进行decode转换成unicode 2.然后使用encode转换成utf-8格式. ...
- MySQL常用语句
第1章 SQL结构化语言 1.什么是SQL? SQL,英文全称Structured Query Language,中文意思是结构化查询语言,它是一种关系型数据库中的数据进行定义和操作的语言方法,是大多 ...
- [转]使用 Shell 对进程资源进行监控
原文:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-monitoring/ 使用 Shell 对进程资源进行监控 检查进程是否存在 在 对 ...
- Excel通过身份证获取出生年月,性别,年龄,生肖,星座,省份等信息总结归纳
Excel通过身份证获取出生年月,性别,年龄,生肖,星座,省份等信息总结归纳 早期的身份证号码为15位数字,现在使用的身份证号码为18位数字,它们的含义如下:(1)15位:1-6位为地区代码,7-8位 ...
- Oracle 截取字符串
如下有一个创建函数的代码,是将一穿字符串按照逗号‘,’分割成若干段 create or replace function SplitStringByComma(aName in varchar2) r ...
- wordcount 过程
hdfs原始数据 hello a hello b map阶段: 输入数据:<0,"hello a"> <8,"hello b"> key ...
- Web String path问题
request.getContextPath()"下方出现了红色的波浪线,提示的错误信息是 "The method getContextPath() from the type H ...
- Linux帮助命令
帮助命令:man man 命令 获取命令的帮助 如下: [root@localhost ~]# man ls LS(1) User Commands LS(1) NAME ls - list dire ...