1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    6. <title>页面名称</title>
    7. </head>
    8. <body>
    9. <textarea id="text" rows="30" cols="220"> </textarea><br />
    10. <input type="button" value="统计" onclick="word();" /><br />
    11. <script type="text/javascript">
    12. var text = document.getElementById("text");
    13. function word() {
    14. var arr = text.value.match(/[\w\-]+/g) || [];
    15. console.log(arr);
    16. var k = {},
    17. p = {};
    18. for (var i = 0; i < arr.length; i++) {
    19. var v = arr[i].toLowerCase();
    20. if (k[v]) {
    21. k[v]++;
    22. } else {
    23. k[v] = 1;
    24. }
    25. }
    26. function sortObj(obj) {
    27. var arr = [];
    28. for (var i in obj) {
    29. arr.push([obj[i], i]);
    30. }
    31. arr.sort(function (a, b) {
    32. return b[0] - a[0];
    33. });
    34. var len = arr.length,
    35. obj = {};
    36. for (var i = 0; i < len; i++) {
    37. obj[arr[i][1]] = arr[i][0];
    38. }
    39. return obj;
    40. }
    41. k = sortObj(k);
    42. function f1(k) {
    43. if (k == null) {
    44. k = {};
    45. }
    46. var title = ["单词", "个数"];
    47. console.log(k);
    48. JSONToExcelConvertor(k, "report", title);
    49. }
    50. f1(k);
    51. }
    52. </script>
    53. <script>
    54. function JSONToExcelConvertor(JSONData, FileName, title, filter) {
    55. if (!JSONData) return;
    56. //转化json为object
    57. var arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData;
    58. var excel = "<table>";
    59. //设置表头
    60. var row = "<tr>";
    61. if (title) {
    62. //使用标题项
    63. for (var i in title) {
    64. row += "<th align='center'>" + title[i] + "</th>";
    65. }
    66. } else {
    67. //不使用标题项
    68. for (var i in arrData[0]) {
    69. row += "<th align='center'>" + i + "</th>";
    70. }
    71. }
    72. excel += row + "</tr>";
    73. var row = "";
    74. for (var index in arrData) {
    75. row += "<tr>";
    76. row += "<td align='center'>" + index + "</td>";
    77. row += "<td align='center'>" + arrData[index] + "</td>";
    78. row += "</tr>";
    79. }
    80. excel += row;
    81. excel += "</table>";
    82. var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
    83. excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
    84. excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
    85. excelFile += '; charset=UTF-8">';
    86. excelFile += "<head>";
    87. excelFile += "<!--[if gte mso 9]>";
    88. excelFile += "<xml>";
    89. excelFile += "<x:ExcelWorkbook>";
    90. excelFile += "<x:ExcelWorksheets>";
    91. excelFile += "<x:ExcelWorksheet>";
    92. excelFile += "<x:Name>";
    93. excelFile += "{worksheet}";
    94. excelFile += "</x:Name>";
    95. excelFile += "<x:WorksheetOptions>";
    96. excelFile += "<x:DisplayGridlines/>";
    97. excelFile += "</x:WorksheetOptions>";
    98. excelFile += "</x:ExcelWorksheet>";
    99. excelFile += "</x:ExcelWorksheets>";
    100. excelFile += "</x:ExcelWorkbook>";
    101. excelFile += "</xml>";
    102. excelFile += "<![endif]-->";
    103. excelFile += "</head>";
    104. excelFile += "<body>";
    105. excelFile += excel;
    106. excelFile += "</body>";
    107. excelFile += "</html>";
    108. var uri = "data:application/vnd.ms-excel;charset=utf-8," + encodeURIComponent(excelFile);
    109. var link = document.createElement("a");
    110. link.href = uri;
    111. link.style = "visibility:hidden";
    112. link.download = FileName + ".xls";
    113. document.body.appendChild(link);
    114. link.click();
    115. document.body.removeChild(link);
    116. }
    117. </script>
    118. </body>
    119. </html>

    image.png