Last active
April 24, 2021 01:46
-
-
Save taewo/ff0f0e804b732b01089715dce1ec4bb3 to your computer and use it in GitHub Desktop.
한글 인코딩 가능한 csv 다운로드 기능
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<h1>The script element</h1> | |
<p id="demo"></p> | |
<button onclick="handleclick()">aaaa</button> | |
<script> | |
document.getElementById("demo").innerHTML = "Hello JavaScript!"; | |
function handleclick (){ | |
console.log('DownloadJsonData') | |
DownloadJsonData(data, 'title', 'label') | |
} | |
var DownloadJsonData = function DownloadJsonData(JSONData, FileTitle, ShowLabel) { | |
console.log(333) | |
var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData) : JSONData; | |
var CSV = ''; | |
if (ShowLabel) { | |
var row = ''; | |
for (var index in arrData[0]) { | |
row += index + ','; | |
} | |
row = row.slice(0, -1); | |
CSV += row + '\r\n'; | |
} | |
for (var i = 0; i < arrData.length; i++) { | |
var row = ''; | |
for (var index in arrData[i]) { | |
row += '"' + arrData[i][index] + '",'; | |
} | |
row.slice(0, row.length - 1); | |
CSV += row + '\r\n'; | |
} | |
if (CSV == '') { | |
alert('Invalid data'); | |
return; | |
} | |
var filename = FileTitle; | |
var BOM = '\ufeff' | |
var csvData = BOM + CSV | |
var blob = new Blob([csvData], { | |
type: 'text/csv;charset=utf-8;' | |
}); | |
if (navigator.msSaveBlob) { | |
navigator.msSaveBlob(blob, filename); | |
} else { | |
var link = document.createElement('a'); | |
if (link.download !== undefined) { | |
var url = URL.createObjectURL(blob); | |
link.setAttribute('href', url); | |
link.style = 'visibility:hidden'; | |
link.download = filename + '.csv'; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
} | |
}; | |
var data = [ | |
{ | |
"status": "False", | |
"rcb": "SSIEC_STMX", | |
"이름": "태웅아아턍IED", | |
"updevice": null, | |
"mmstime": "2021-01-22 11:17:49.591", | |
"sendcount": 158, | |
"notcount": 0, | |
"maxcycle": 2958, | |
"mincycle": 1227 | |
}, | |
{ | |
"status": "False", | |
"rcb": "D308_C4A7_RCB_Meter", | |
"이름": '아아아아앙아', | |
"updevice": null, | |
"mmstime": "2021-01-21 09:59:35.481", | |
"sendcount": 10809, | |
"notcount": 2, | |
"maxcycle": 15257, | |
"mincycle": 3030 | |
}, | |
{ | |
"status": "False", | |
"rcb": "D308_C2MTR_C_MX", | |
"이름": '안녕어어어엉', | |
"updevice": null, | |
"mmstime": "2021-01-21 09:59:35.204", | |
"sendcount": 10810, | |
"notcount": 2, | |
"maxcycle": 14979, | |
"mincycle": 3266 | |
} | |
] | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment