index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 zhCN from 'x-data-spreadsheet/dist/locale/zh-cn';
  28. import XLSX from 'xlsx';
  29. export default {
  30. name: "Sheet",
  31. props: {
  32. value: {
  33. type: [Object, Array],
  34. default: function () {
  35. return {}
  36. }
  37. },
  38. readOnly: {
  39. type: Boolean,
  40. default: false
  41. },
  42. },
  43. data() {
  44. return {
  45. sheet: null,
  46. clientHeight: 0,
  47. clientWidth: 0,
  48. bakValue: '',
  49. }
  50. },
  51. mounted() {
  52. Spreadsheet.locale('zh-cn', zhCN);
  53. //
  54. let options = {
  55. view: {
  56. height: () => {
  57. try {
  58. return this.clientHeight = this.$refs.xspreadsheet.clientHeight;
  59. }catch (e) {
  60. return this.clientHeight;
  61. }
  62. },
  63. width: () => {
  64. try {
  65. return this.clientWidth = this.$refs.xspreadsheet.clientWidth;
  66. }catch (e) {
  67. return this.clientWidth;
  68. }
  69. },
  70. },
  71. };
  72. if (this.readOnly) {
  73. options.mode = 'read'
  74. options.showToolbar = false
  75. options.showContextmenu = false;
  76. }
  77. this.bakValue = JSON.stringify(this.value);
  78. this.sheet = new Spreadsheet(this.$refs.xspreadsheet, options).loadData(this.value).change(data => {
  79. if (!this.readOnly) {
  80. this.bakValue = JSON.stringify(this.sheet.getData());
  81. this.$emit('input', this.sheet.getData());
  82. }
  83. });
  84. //
  85. this.sheet.validate()
  86. },
  87. watch: {
  88. value: {
  89. handler(value) {
  90. if (this.bakValue == JSON.stringify(value)) {
  91. return;
  92. }
  93. this.bakValue = JSON.stringify(value);
  94. this.sheet.loadData(value);
  95. },
  96. deep: true
  97. }
  98. },
  99. methods: {
  100. exportExcel(name, bookType){
  101. var new_wb = this.xtos(this.sheet.getData());
  102. XLSX.writeFile(new_wb, name + "." + (bookType == 'xlml' ? 'xls' : bookType), {
  103. bookType: bookType || "xlsx"
  104. });
  105. },
  106. xtos(sdata) {
  107. var out = XLSX.utils.book_new();
  108. sdata.forEach(function(xws) {
  109. var aoa = [[]];
  110. var rowobj = xws.rows;
  111. for(var ri = 0; ri < rowobj.len; ++ri) {
  112. var row = rowobj[ri];
  113. if(!row) continue;
  114. aoa[ri] = [];
  115. Object.keys(row.cells).forEach(function(k) {
  116. var idx = +k;
  117. if(isNaN(idx)) return;
  118. aoa[ri][idx] = row.cells[k].text;
  119. });
  120. }
  121. var ws = XLSX.utils.aoa_to_sheet(aoa);
  122. XLSX.utils.book_append_sheet(out, ws, xws.name);
  123. });
  124. return out;
  125. },
  126. }
  127. }
  128. </script>