2022-09-05 08:26:25
<html>
<head>
<title> Download CSV file </title>
</head>
<script>
var csvFileData = [
['Alan Walker', 'Singer'],
['Cristiano Ronaldo', 'Footballer'],
['Saina Nehwal', 'Badminton Player'],
['Arijit Singh', 'Singer'],
['Terence Lewis', 'Dancer']
];
function download_csv_file() {
var csv = 'Name,Profession\n';
csvFileData.forEach(function(row) {
csv += row.join(',');
csv += "\n";
});
document.write(csv);
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = 'Famous Personalities.csv';
hiddenElement.click();
}
</script>
<body>
<h3> Click the button to download the CSV file </h3>
<button onclick="download_csv_file()"> Download CSV </button>
</body>
</html>
文章来源:https://www.javatpoint.com/javascript-create-and-download-csv-file
最后生成于 2023-06-27 21:38:24
热门推荐: