1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://unpkg.com/xlsx@0.18.5"></script> <title>json to excel</title> </head> <body> <button onclick="toExcel()">导出</button>
<script> const demo = [{ name: "电量", cusp: "111", peak: "222", flat: "333", grain: "444" }, { name: "电费", cusp: "111", peak: "222", flat: "333", grain: "444" }, { name: "服务费", cusp: "111", peak: "222", flat: "333", grain: "444" } ];
function toExcel() { const fileName = "json导出的例子.xlsx"; const sheetName = "Sheet 1";
const excel = XLSX.utils.book_new(); const data = XLSX.utils.json_to_sheet(demo);
XLSX.utils.book_append_sheet(excel, data, sheetName); XLSX.writeFile(excel, fileName); } </script> </body> </html>
|