直接上代码:
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>javascript</title> <meta name="generator" content="EverEdit" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <script type="text/javascript"> function exportcsv(alink) { var str="字段1,字段2,字段3\naaa,bbb,ccc"; str=encodeURIComponent(str); alink.href="data:text/csv;charset=utf-8,"+str; } </script> </head> <body> <a download="test.csv" οnclick="exportcsv(this)" href="#">导出CSV文件</a> </body> </html>
转载于:https://blog.51cto.com/suiwnet/1599671