码迷,mamicode.com
首页 > 编程语言 > 详细

Java Js实现导出csv

时间:2016-06-15 10:28:31      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

 1 $.ajax({
 2     type: ‘GET‘,
 3     async: false,
 4     url: ‘../../api/screening/exportTable?seriesIndex=‘ + param.seriesIndex +‘&dataIndex=‘ + param.dataIndex + ‘&package1=‘ + mode,
 5     success: function(data) {
 6         JSONToCSVConvertor(JSON.stringify(data), title, true);
 7     },
 8     error: function(XMLHttpRequest) {
 9         $.messager.show({msg:XMLHttpRequest}); 
10     }
11 })

 

网上抄的方法

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

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

 

后台生产JSONArray

 1 @RequestMapping(value = "/exportTable", method = RequestMethod.GET)
 2     public @ResponseBody JSONArray exportTable(HttpServletRequest request,
 3             HttpServletResponse response, String seriesIndex, String dataIndex, String package1) throws Exception {
 4         Map<String, Object> map = new HashMap<String, Object>();
 5         map = getAnalyzingSelectedList(request, response, seriesIndex, dataIndex, null, package1);
 6         Iterable<DmMeasureBean> list= (List<DmMeasureBean>)map.get("rows");
 7         JSONArray jsonArray = new JSONArray();
 8         JSONObject jsonObject = new JSONObject();  
 9         for(DmMeasureBean bean : list) {
10             jsonObject.clear();
11             jsonObject.put("**", bean.getName());
12             jsonObject.put("**", bean.getAge());
13             jsonObject.put("**", bean.getGenderString());
14             jsonObject.put("**", new SimpleDateFormat("yyyy-MM-dd").format(bean.getBirthday()));
15             jsonObject.put("**", bean.getResult() == null ? "" : bean.getResult());
16             jsonObject.put("**", bean.getExamDate() == null ? "" : new SimpleDateFormat("yyyy-MM-dd").format(bean.getExamDate()));
17             jsonObject.put("**", bean.getTel());
18             jsonArray.add(jsonObject);
19         } 
20         return jsonArray;
21     }

 

Java Js实现导出csv

标签:

原文地址:http://www.cnblogs.com/zziy/p/5586404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!