index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div ref="xspreadsheet" class="xspreadsheet" :class="{'xspreadsheet-readonly':readOnly}"></div>
  3. </template>
  4. <style lang="scss">
  5. .xspreadsheet-readonly {
  6. .x-spreadsheet-menu {
  7. > li:first-child {
  8. > div.x-spreadsheet-icon {
  9. display: none;
  10. }
  11. }
  12. }
  13. }
  14. </style>
  15. <style lang="scss" scoped>
  16. .xspreadsheet {
  17. position: absolute;
  18. top: 0;
  19. left: 0;
  20. width: 100%;
  21. height: 100%;
  22. display: flex;
  23. }
  24. </style>
  25. <script>
  26. import Spreadsheet from 'x-data-spreadsheet';
  27. import EN from 'x-data-spreadsheet/src/locale/en';
  28. import zhCN from 'x-data-spreadsheet/src/locale/zh-cn';
  29. import XLSX from 'xlsx';
  30. export default {
  31. name: "Sheet",
  32. props: {
  33. value: {
  34. type: [Object, Array],
  35. default: function () {
  36. return {}
  37. }
  38. },
  39. readOnly: {
  40. type: Boolean,
  41. default: false
  42. },
  43. },
  44. data() {
  45. return {
  46. sheet: null,
  47. clientHeight: 0,
  48. clientWidth: 0,
  49. bakValue: '',
  50. }
  51. },
  52. mounted() {
  53. if (this.getLanguage() === 'zh') {
  54. Spreadsheet.locale('zh-cn', zhCN);
  55. } else {
  56. Spreadsheet.locale('en', EN);
  57. }
  58. //
  59. let options = {
  60. view: {
  61. height: () => {
  62. try {
  63. return this.clientHeight = this.$refs.xspreadsheet.clientHeight;
  64. }catch (e) {
  65. return this.clientHeight;
  66. }
  67. },
  68. width: () => {
  69. try {
  70. return this.clientWidth = this.$refs.xspreadsheet.clientWidth;
  71. }catch (e) {
  72. return this.clientWidth;
  73. }
  74. },
  75. },
  76. };
  77. if (this.readOnly) {
  78. options.mode = 'read'
  79. options.showToolbar = false
  80. options.showContextmenu = false;
  81. }
  82. this.bakValue = JSON.stringify(this.value);
  83. this.sheet = new Spreadsheet(this.$refs.xspreadsheet, options).loadData(this.value).change(data => {
  84. if (!this.readOnly) {
  85. this.bakValue = JSON.stringify(this.sheet.getData());
  86. this.$emit('input', this.sheet.getData());
  87. }
  88. });
  89. //
  90. this.sheet.validate()
  91. },
  92. watch: {
  93. value: {
  94. handler(value) {
  95. if (this.bakValue == JSON.stringify(value)) {
  96. return;
  97. }
  98. this.bakValue = JSON.stringify(value);
  99. this.sheet.loadData(value);
  100. },
  101. deep: true
  102. }
  103. },
  104. methods: {
  105. exportExcel(name, bookType){
  106. var new_wb = this.xtos(this.sheet.getData());
  107. XLSX.writeFile(new_wb, name + "." + (bookType == 'xlml' ? 'xls' : bookType), {
  108. bookType: bookType || "xlsx"
  109. });
  110. },
  111. xtos(sdata) {
  112. var out = XLSX.utils.book_new();
  113. sdata.forEach(function(xws) {
  114. var aoa = [[]];
  115. var rowobj = xws.rows;
  116. for(var ri = 0; ri < rowobj.len; ++ri) {
  117. var row = rowobj[ri];
  118. if(!row) continue;
  119. aoa[ri] = [];
  120. Object.keys(row.cells).forEach(function(k) {
  121. var idx = +k;
  122. if(isNaN(idx)) return;
  123. aoa[ri][idx] = row.cells[k].text;
  124. });
  125. }
  126. var ws = XLSX.utils.aoa_to_sheet(aoa);
  127. XLSX.utils.book_append_sheet(out, ws, xws.name);
  128. });
  129. return out;
  130. },
  131. }
  132. }
  133. </script>