[jQuery] json 데이터를 엑셀(CSV)로 익스포트

Posted by RAY.D
2015. 4. 28. 09:34 Web/javascript / jQuery
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.




Convert JSON format to CSV format for MS Excel [closed]





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    
<title>JSON to CSV</title>
    
<script src="scripts/json.js" type="text/javascript"></script>
    
<script type="text/javascript">
    
var json3 = { "d""[{\"Id\":1,\"UserName\":\"Sam Smith\"},{\"Id\":2,\"UserName\":\"Fred Frankly\"},{\"Id\":1,\"UserName\":\"Zachary Zupers\"}]" }

DownloadJSON2CSV(json3.d);

function DownloadJSON2CSV(objArray)
    {
        
var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;

var str = '';

for (var i = 0; i < array.length; i++) {
            
var line = '';

for (var index in array[i]) {
                line += array[i][index] + 
',';
            }

// Here is an example where you would wrap the values in double quotes
            
// for (var index in array[i]) {
            
//    line += '"' + array[i][index] + '",';
            
// }

line.slice(0,line.Length-1);

str += line + '\r\n';
        }
        window.open( 
"data:text/csv;charset=utf-8," + escape(str))
    }

</script>

</head>
<body>
    
<h1>This page does nothing....</h1>
</body>
</html>

 

원본 위치 <http://stackoverflow.com/questions/4130849/convert-json-format-to-csv-format-for-ms-excel