Java Js实现导出csv

 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     }

 

转载于:https://www.cnblogs.com/zziy/p/5586404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值