1 $.ajax ({2Type: ' GET ',3Asyncfalse,4URL: '.. /.. /api/screening/exporttable?seriesindex= ' + param.seriesindex + ' &dataindex= ' + param.dataindex + ' &package1= ' +mode,5Successfunction(data) {6Jsontocsvconvertor (json.stringify (data), title,true);7 },8Errorfunction(XMLHttpRequest) {9 $.messager.show ({msg:xmlhttprequest});Ten } One})
How to copy Online
var uri = ' Data:text/csv;charset=utf-8,\ufeff ' + encodeuri (CSV);
1 functionJsontocsvconvertor (Jsondata, Reporttitle, ShowLabel) {2 //If Jsondata is not a object then Json.parse would parse the JSON string in an object3 varArrdata =typeofJsondata! = ' object '?Json.parse (jsondata): Jsondata;4 varCSV = ";5 //This condition'll generate the Label/header6 if(ShowLabel) {7 varrow = "";8 //This loop would extract the label from 1st Index of an on array9 for(varIndexinchArrdata[0]) {Ten //Now convert each value to string and comma-seprated OneRow + = index + ', '; A } -row = Row.slice (0,-1); - //append Label row with line break theCSV + = row + ' \ r \ n '; - } - //1st Loop is to extract each row - for(vari = 0; i < arrdata.length; i++) { + varrow = ""; - //2nd loop would extract each column and convert it in string comma-seprated + for(varIndexinchArrdata[i]) { ARow + = ' "' + Arrdata[i][index] + '", '; at } -Row.slice (0, Row.length-1); - //Add a line break after each row -CSV + = row + ' \ r \ n '; - } - if(CSV = = ") { inAlert ("Invalid data"); - return; to } + //Generate a file name - varFileName = "Export Table _"; the //This would remove the blank-spaces from the title and replace it with an underscore *FileName + = Reporttitle.replace (//g, "_"); $ //Initialize file format you want CSV or XLSPanax Notoginseng varURI = ' Data:text/csv;charset=utf-8,\ufeff ' +encodeURI (CSV); - //Now the little tricky part. the //You can use either>> window.open (URI); + //But this would not be work in some browsers A //Or you'll not get the correct file extension the //This trick would generate a temp <a/> tag + varlink = document.createelement ("a"); -Link.href =URI; $ //set the visibility hidden so it won't effect on your web-layout $Link.style = "Visibility:hidden"; -Link.download = FileName + ". csv"; - //This part would append the anchor tag and remove it after automatic click the document.body.appendChild (link); - Link.click ();Wuyi document.body.removeChild (link); the}
Backstage Production 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 {4map<string, object> map =NewHashmap<string, object>();5Map = getanalyzingselectedlist (Request, Response, Seriesindex, Dataindex,NULL, package1);6Iterable<dmmeasurebean> list= (list<dmmeasurebean>) map.get ("Rows");7Jsonarray Jsonarray =NewJsonarray ();8Jsonobject Jsonobject =NewJsonobject (); 9 for(Dmmeasurebean bean:list) {Ten jsonobject.clear (); OneJsonobject.put ("* *", Bean.getname ()); AJsonobject.put ("* *", Bean.getage ()); -Jsonobject.put ("* *", bean.getgenderstring ()); -Jsonobject.put ("* *",NewSimpleDateFormat ("Yyyy-mm-dd"). Format (Bean.getbirthday ())); theJsonobject.put ("* *", bean.getresult () = =NULL? "": Bean.getresult ()); -Jsonobject.put ("* *", bean.getexamdate () = =NULL? "" :NewSimpleDateFormat ("Yyyy-mm-dd"). Format (Bean.getexamdate ())); -Jsonobject.put ("* *", Bean.gettel ()); - Jsonarray.add (jsonobject); + } - returnJsonarray; +}
Java JS Implementation Export CSV