text.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <html>
  2. <head>
  3. <title>echarts 力导向图</title>
  4. <meta charset="UTF-8">
  5. <!-- <script type="text/javascript" src="js/jquery-1.9.0.min.js" ></script> -->
  6. <script src="/static/js/jquery.min.js"></script>
  7. <script type="text/javascript" src="https://cdn.staticfile.org/xlsx/0.15.1/xlsx.core.min.js"></script>
  8. <script src="/static/js/vue-2.4.0.js"></script>
  9. </head>
  10. <body>
  11. <div id="app">
  12. <input type="file" @change="tirggerFile($event)">
  13. <button @click="aa()">上传</button>
  14. </div>
  15. </body>
  16. <script>
  17. var vm = new Vue({
  18. el: '#app',
  19. data: {
  20. data_timeqi: '', //查询时间起
  21. data_timezhi: '', //查询时间止
  22. },
  23. mounted() {
  24. },
  25. methods: {
  26. aa() {
  27. console.log(11111)
  28. window.onload = function () {
  29. $('#excel-file').change(function (e) {
  30. var files = e.target.files;
  31. var fileReader = new FileReader();
  32. fileReader.onload = function (ev) {
  33. try {
  34. var data = ev.target.result
  35. var workbook = XLSX.read(data, {
  36. type: 'binary'
  37. }); // 以二进制流方式读取得到整份excel表格对象
  38. persons = [];
  39. } catch (e) {
  40. alertMsg(2, '文件类型不正确, 请重新选择文件');
  41. return;
  42. }
  43. // 表格的表格范围,可用于判断表头是否数量是否正确
  44. var fromTo = '';
  45. // 遍历每张表读取
  46. for (var sheet in workbook.Sheets) {
  47. if (workbook.Sheets.hasOwnProperty(sheet)) {
  48. fromTo = workbook.Sheets[sheet]['!ref'];
  49. if (!fromTo) {
  50. alert(2, '文件内容为空, 导入失败');
  51. return;
  52. }
  53. persons = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]);
  54. // break; // 如果只取第一张表,就取消注释这行
  55. }
  56. }
  57. console.log(persons);
  58. //将persons赋值给要展示的表格数组里即可展示在界面
  59. };
  60. // 以二进制方式打开文件
  61. if (files.length == 0) {
  62. return;
  63. }
  64. fileReader.readAsBinaryString(files[0]);
  65. e.target.value = '';
  66. });
  67. }
  68. },
  69. tirggerFile(e) {
  70. var files = e.target.files;
  71. var fileReader = new FileReader();
  72. fileReader.onload = function (ev) {
  73. try {
  74. var data = ev.target.result
  75. var workbook = XLSX.read(data, {
  76. type: 'binary'
  77. }); // 以二进制流方式读取得到整份excel表格对象
  78. persons = [];
  79. } catch (e) {
  80. alertMsg(2, '文件类型不正确, 请重新选择文件');
  81. return;
  82. }
  83. // 表格的表格范围,可用于判断表头是否数量是否正确
  84. var fromTo = '';
  85. // 遍历每张表读取
  86. for (var sheet in workbook.Sheets) {
  87. if (workbook.Sheets.hasOwnProperty(sheet)) {
  88. fromTo = workbook.Sheets[sheet]['!ref'];
  89. if (!fromTo) {
  90. alert(2, '文件内容为空, 导入失败');
  91. return;
  92. }
  93. persons = XLSX.utils.sheet_to_json(workbook.Sheets[sheet]);
  94. // break; // 如果只取第一张表,就取消注释这行
  95. }
  96. }
  97. console.log(persons);
  98. //将persons赋值给要展示的表格数组里即可展示在界面
  99. };
  100. // 以二进制方式打开文件
  101. if (files.length == 0) {
  102. return;
  103. }
  104. fileReader.readAsBinaryString(files[0]);
  105. e.target.value = '';
  106. }
  107. },
  108. created() {
  109. }
  110. });
  111. </script>
  112. </html>